php 如何检查 SELECT EXISTS 是否返回值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4484974/
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
How to check whether SELECT EXISTS returns a value or not?
提问by supermitch
I am trying to quickly determine if a user_ID is the owner of a 'goal'. I believe my SQL query is good, but I'm trying to find a nice way of checking the result!
我正在尝试快速确定 user_ID 是否是“目标”的所有者。我相信我的 SQL 查询很好,但我正在尝试找到一种检查结果的好方法!
In this case, no matter what I put for $obj_id or $user_id, my function returns true. I assume it's because mysql_num_rows is counting even a false result as a row? So what PHP code should I use to check to see if the result exists or not?
在这种情况下,无论我为 $obj_id 或 $user_id 设置什么,我的函数都会返回 true。我认为这是因为 mysql_num_rows 甚至将错误结果计算为一行?那么我应该使用什么 PHP 代码来检查结果是否存在?
Note that I want something short and elegant! I know I could do it the long way (check count(*), return mysql_assoc then check the count value...) but that is long winded and ugly.
请注意,我想要一些简短而优雅的东西!我知道我可以做很长的路(检查计数(*),返回 mysql_assoc 然后检查计数值......)但这是冗长而丑陋的。
Any ideas? Thanks!
有任何想法吗?谢谢!
$query = "SELECT EXISTS (SELECT * FROM goals WHERE goal_ID='$obj_id' AND user_ID='$user_id')";
if (@mysql_num_rows(mysql_query($query))!=1) {
return false;
} else {
return true;
}
回答by gbn
Don't bother with EXISTS. The in-line exists will always give onerow containing "true" or "false".
不要打扰 EXISTS。直列式存在总会给一个包含行“真”或“假”。
You're looking for either "zero rows" or "at least one row" so change the query to something like this and thencheck how many rows are returned
您正在寻找“零行”或“至少一行”,因此将查询更改为这样的内容,然后检查返回了多少行
SELECT 1 FROM goals WHERE goal_ID='$obj_id' AND user_ID='$user_id' LIMIT 1
回答by Brad Mace
I like gbn's answer the best, but I wanted to point out that this:
我最喜欢 gbn 的回答,但我想指出这一点:
if (@mysql_num_rows(mysql_query($query))!=1) {
return false;
} else {
return true;
}
can be simplified to:
可以简化为:
return @mysql_num_rows(mysql_query($query)) == 1;
回答by Esteban
Counting how many rows match the criteria should be easier:
计算符合条件的行数应该更容易:
$sql = SELECT COUNT(*) FROM goals WHERE goal_ID='$obj_id' AND user_ID='$user_id'
$query = mysql_query($sql);
$result = mysql_fetch_row($query);
return $result[0] >= 1;
回答by Bas
This way is probably faster.
这种方式可能更快。
$query = "SELECT EXISTS (SELECT * FROM goals WHERE goal_ID='$obj_id' AND user_ID='$user_id')";
if(mysql_num_rows(mysqli_query($query)) < 1) {
// Nothing found!
}
回答by Siebe Jongebloed
SELECT EXISTS always returns a row! The following code uses the fastest MySql query(according to this answer: https://stackoverflow.com/a/10688065/3710053) and gives in PHP the correct result:
SELECT EXISTS 总是返回一行!以下代码使用最快的 MySql 查询(根据此答案:https: //stackoverflow.com/a/10688065/3710053)并在 PHP 中给出正确的结果:
$link = mysqli_connect($DB_SERV, $DB_USER, $DB_PASS, $DB_NAME);
$query = "SELECT EXISTS (SELECT * FROM goals
WHERE goal_ID='$obj_id'
AND user_ID='$user_id'
LIMIT 1)
as `row_exists`";
if(mysqli_fetch_assoc(mysqli_query($link,$query))['row_exists'] ===0) {
// Nothing found!
}
回答by levent
mysql_result(mysql_query("SELECT EXISTS (SELECT * FROM goals WHERE goal_ID='$obj_id' AND user_ID='$user_id')"),0);
回答by tareco
what about this:
那这个呢:
$query = "SELECT EXISTS (SELECT * FROM goals WHERE goal_ID='$obj_id' AND user_ID='$user_id')";
return mysql_query($query) ? false : true;