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是否是“goal”的所有者。我相信我的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.
注意,我想要一些短小而优雅的东西!我知道我可以做很长一段时间(检查count(*),返回mysql_assoc,然后检查count值…),但是这很长,而且很难看。
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;
}
6 个解决方案
#1
42
Don't bother with EXISTS. The in-line exists will always give one row containing "true" or "false".
别烦与存在。in-line始终给出包含“true”或“false”的一行。
You're looking for either "zero rows" or "at least one row" so change the query to something like this and then check how many rows are returned
您正在寻找“零行”或“至少一行”,因此将查询更改为类似的内容,然后检查返回了多少行
SELECT 1 FROM goals WHERE goal_ID='$obj_id' AND user_ID='$user_id' LIMIT 1
#2
10
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;
#3
2
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!
}
#4
2
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;
#5
0
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;
#6
0
mysql_result(mysql_query("SELECT EXISTS (SELECT * FROM goals WHERE goal_ID='$obj_id' AND user_ID='$user_id')"),0);
#1
42
Don't bother with EXISTS. The in-line exists will always give one row containing "true" or "false".
别烦与存在。in-line始终给出包含“true”或“false”的一行。
You're looking for either "zero rows" or "at least one row" so change the query to something like this and then check how many rows are returned
您正在寻找“零行”或“至少一行”,因此将查询更改为类似的内容,然后检查返回了多少行
SELECT 1 FROM goals WHERE goal_ID='$obj_id' AND user_ID='$user_id' LIMIT 1
#2
10
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;
#3
2
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!
}
#4
2
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;
#5
0
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;
#6
0
mysql_result(mysql_query("SELECT EXISTS (SELECT * FROM goals WHERE goal_ID='$obj_id' AND user_ID='$user_id')"),0);