[英]Wrong SQL Result Condition
I am having a bit of a problem with some code where a have a query which should come back false and it's returning true. 我对某些代码有一些问题,其中有一个查询应该返回false,并且返回true。
First I have an sql query to an id that does not exist: 首先,我有一个SQL查询到一个不存在的ID:
$result = mysqli_query($con,"SELECT * FROM myTable WHERE id='1' "); //There no id with value of 1
Then I've added a var_dump to see what's on: 然后,我添加了一个var_dump以查看发生了什么:
var_dump($result);
and after a condition: 在一个条件之后:
if ($result==0) {
echo 'No results where found';
} else {
echo 'Some results where found';
}
Here's the result of the var_dump 这是var_dump的结果
object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(12) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }
Some results where found 找到一些结果
The result should be: No results where found and as you can see above, it's return: Some results where found 结果应该是:找不到结果,如上所示,返回:找到了一些结果
What I'm I doing wrong here? 我在这里做错了什么?
You need mysqli_num_rows()
to see if the query has some rows something as 您需要mysqli_num_rows()
来查看查询是否有一些行,例如
$count=mysqli_num_rows($result);
if($count == 0){
// no result
}else{
// there is data and do whatever you want
}
Check the documentation of mysqli_query()
and the return part http://in2.php.net/mysqli_query 检查mysqli_query()
的文档和返回部分http://in2.php.net/mysqli_query
Returns FALSE on failure. 失败时返回FALSE。 For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. 对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,mysqli_query()将返回mysqli_result对象。 For other successful queries mysqli_query() will return TRUE. 对于其他成功的查询,mysqli_query()将返回TRUE。
I think that your id
field is an integer
, so: 我认为您的id
字段是一个integer
,所以:
SELECT * FROM myTable WHERE id=1
should work 应该管用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.