简体   繁体   English

检查值是否存在于MySQL行中

[英]Check if value exists In MySQL row

I have a php variable: $foo 我有一个php变量: $foo

My MySQL table called data has the following structure: 我的名为data MySQL表具有以下结构:

id    var    header

1     zj3     http://google.com

I would like to check if $foo is all ready in var row. 我想检查$foo是否已准备好在var行中。

If it is I would like to echo header ("http://google.com") 如果是的话我想回复header (“http://google.com”)

How would you approach this? 你会怎么做?

Thanks in advance, please ask if any clarification is needed! 在此先感谢,请询问是否需要澄清!

Your query should be: 您的查询应该是:

SELECT `header` FROM `data` WHERE `var` = '$foo'

This will return all the headers with a var value of $foo . 这将返回var值为$foo所有标头。

$db = mysqli_connect('localhost', 'username', 'password', 'database');
if($query = mysqli_query($db, "SELECT `header` FROM `data` WHERE `var` = '$foo'")){
  while($row = mysqli_fetch_assoc($query)){
    echo $row['header'];
  }
  mysqli_free_result($query);
}

first connect to the db 首先连接到db

$query = mysql_query("SELECT var, header FROM data WHERE id='1'") or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
    if($foo == $row['var']){
        echo $row['header'];
    }
}

EDIT: changed equality statement based on your edit 编辑:根据您的编辑更改了相等的声明

are you asking if $foo matches any of the fields in data , or if $foo=some_field ? 你在问$foo匹配data中的任何字段,或者$foo=some_field Here for if you want $foo==var . 在这里,如果你想要$foo==var

$foo='somevalue';
$query="SELECT id, var, header FROM `data` WHERE var='$foo'";
$result=mysqli_query($query);
if($result->num_rows==0) 
  $loc= 'http://google.com';//default value for when there is no row that matches $foo
}else{
  $row=$result->fetch_assoc(); //more than one row is useless since the first header('Location: x') command sends the browser to a new page and away from your script.
  $loc=$row['header'];

}
header ("Location: $loc);
exit;

ETA: since you've edited your question, it appears that you want to echo the header column if your search value matches your var column. ETA:由于您已编辑了问题,因此如果您的搜索值与var列匹配,则您希望回显标题列。 The above won't work for that. 以上内容不适合。

It's not difficult at all, If I understand correctly then this should help you. 这根本不难,如果我理解正确,那么这应该对你有所帮助。

// Query Variable / Contains you database query information
$results = $query;

// Loop through like so if the results are returned as an array
foreach($results as $result)
{
    if(!$result['var'])
        echo $result['header'];
}

// Loop through like so if the results are returned as an object
foreach($results as $result)
{
    if(!$result->var)
        echo $result->header;
}

Here's a template for all the "does it exist" questions. 这是所有“存在”问题的模板。

This is the only thing that actually worked for me so far and is not deprecated. 到目前为止,这是我唯一真正有用的东西,并没有被弃用。

if ($query = mysqli_query($link, "SELECT header FROM data WHERE var = '$foo'")) {

        $header = mysqli_fetch_assoc($query);

        if ($header) {

            // The variable with value $foo exists.
        }
        else {

            // The variable with value $foo doesn't exist.
        }
    }
    else {

        // The query didn't execute for some reason. (Dammit Obama!)
    }

WARNING! 警告!

Even if the variable DOES NOT EXIST the comparison between $query and mysqli_query() will always return TRUE . 即使变量DOES NOT EXIST$ querymysqli_query()之间的比较也总是返回TRUE

The only way --which happened to me-- for the comparison to return FALSE is because of a syntax error in your query. 唯一的方法 - 发生在我身上 - 因为比较返回FALSE是因为查询中存在语法错误。

I don't know why it worked for the guy who wrote the accepted answer, maybe it's an update or maybe he had a syntax error and was so confident that he didn't check if it could ever be TRUE . 我不知道为什么它对那些写了接受的答案的人有用,也许这是一个更新,或者他有一个语法错误,并且非常自信,他没有检查它是否真的是真的

Here's the comment someone made for correcting his syntax: 这是有人为纠正他的语法所做的评论:

"Add another ) before the { in the first line" 在{在第一行“之前”添加另一个“

So, the accepted answer is WRONG! 所以,接受的答案是错的!

You just want to know if $var's value is anywhere in that column (any row(s))? 您只想知道$ var的值是否在该列中的任何位置(任何行)?

SELECT COUNT(id) FROM data WHERE var = ?;

The result will be the number of rows for which the field var contains the value of $var. 结果将是field var包含$ var值的行数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM