php 检查 MySQL 行中是否存在值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4821270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if value exists In MySQL row
提问by Trufa
I have a php variable: $foo
我有一个 php 变量: $foo
My MySQL table called data
has the following structure:
我的 MySQL 表调用data
具有以下结构:
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!
在此先感谢,请询问是否需要澄清!
回答by Rocket Hazmat
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);
}
回答by heymrcarter
first connect to the 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
编辑:根据您的编辑更改等式语句
回答by dnagirl
are you asking if $foo
matches anyof the fields in data
, or if $foo=some_field
? Here for if you want $foo==var
.
你是问是否$foo
匹配中的任何字段data
,或者如果$foo=some_field
?如果你想要,这里$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. The above won't work for that.
ETA:由于您已经编辑了您的问题,如果您的搜索值与您的 var 列匹配,您似乎想要回显标题列。以上不会为此工作。
回答by brenjt
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;
}
回答by Vulkan
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 EXISTthe comparison between $queryand mysqli_query()will always return TRUE.
即使变量不存在,$query和mysqli_query()之间的比较也将始终返回TRUE。
The only way --which happened to me-- for the comparison to return FALSEis 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.
我不知道为什么它对编写已接受答案的人有用,也许是更新,或者他有语法错误并且非常自信,以至于他没有检查它是否可能为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!
所以,接受的答案是错误的!
回答by Mike
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.
结果将是字段 var 包含 $var 值的行数。