简体   繁体   English

mysql_num_rows无法正常工作

[英]mysql_num_rows not working properly

I'm trying to check if the value of a mysql query returns false or actually contains a value. 我正在尝试检查mysql查询的值是否返回false或实际上包含一个值。 My first solution which worked properly looked like this: 我第一个正常工作的解决方案如下所示:

    $gotTrailer = mysql_query("SELECT trailer FROM film_backup WHERE id='".key($countValues)."'");
    $iHasTrailer = mysql_fetch_array($gotTrailer);
    while(mysql_num_rows($gotTrailer)==''){
        next($countValues);
        $gotTrailer = mysql_query("SELECT trailer FROM film_backup WHERE id='".key($countValues)."'");
        $iHasTrailer = mysql_fetch_array($gotTrailer);
    }

This is a truly ugly solution, so I tried using mysql_num_rows() instead: 这是一个非常丑陋的解决方案,因此我尝试使用mysql_num_rows()代替:

$gotTrailer = mysql_query("SELECT trailer FROM film_backup WHERE id='".key($countValues)."'");
    while(mysql_num_rows($gotTrailer)==0){
        next($countValues);
        $gotTrailer = mysql_query("SELECT trailer FROM film_backup WHERE id='".key($countValues)."'");
    }

But for whatever reason, this simply won't work. 但是无论出于什么原因,这根本行不通。 I tried using === false as well, but I geniunely don't know what's wrong. 我也尝试使用=== false,但是我真的不知道这是怎么回事。 Hope you can help. 希望能对您有所帮助。

mysql_query() will return false only when it errors out. mysql_query()仅在出错时才会返回false If the query returns an empty result, it still returns a resource (not false, empty, or 0). 如果查询返回空结果,则它仍返回资源(非false,空或0)。

mysql_fetch_array() will return false if there are no more rows, otherwise you are returned an array. 如果没有更多的行, mysql_fetch_array()将返回false ,否则将返回一个数组。

On a sidenote, these methods have been deprecated. 附带说明,这些方法已被弃用。

Resources: http://php.net/manual/en/function.mysql-query.php 资源: http : //php.net/manual/en/function.mysql-query.php

http://www.php.net/manual/en/function.mysql-fetch-array.php http://www.php.net/manual/zh/function.mysql-fetch-array.php

mysql_query returns false on error. mysql_query在错误时返回false。 If you want to check if there is an error on the query or not, you should check the return value of that function. 如果要检查查询是否存在错误,则应检查该函数的返回值。

Now, if you want to check if there IS a result or not, mysql_num_rows will return >= 0 or false on error. 现在,如果要检查是否有结果,mysql_num_rows将返回> = 0或在错误时返回false。 So, your while should be 所以,你的时间应该是

 while( (mysql_num_rows($gotTrailer) !== false) ){

Besides all this, the code has other flaws, next() could return false and the mysql_query will fail, also, remember to escape the queries to avoid SQL injection. 除此之外,代码还有其他缺陷,next()可能返回false,并且mysql_query将会失败,另外,请记住对查询进行转义以避免SQL注入。

You can try with a For Loop as well 您也可以尝试使用For Loop

    $gotTrailer = mysql_query("SELECT trailer FROM film_backup WHERE id='".key($countValues)."'");

    for($i=0; $i<mysql_num_rows($gotTrailer); $i++)
    {
       next($countValues);
       $gotTrailer = mysql_query("SELECT trailer FROM film_backup WHERE id='".key($countValues)."'");
    }

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

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