Whats the proper way to check if mysql_query() returned any results?

后端 未结 8 1318
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 17:03

I tried what seemed like the most intuitive approach

$query = \"SELECT * FROM members 
          WHERE username = \'$_CLEAN[username]\'
          AND password =          


        
相关标签:
8条回答
  • 2021-02-05 17:40

    well...

    by definiton mysql_query:

    mysql_query() returns a resource on success, or FALSE on error.

    but what you need to understand is if this function returns a value different than FALSE the query has been ran without problems (correct syntax, connect still alive,etc.) but this doesnt mean you query is returning some row.

    for example

    <?php
    
    $result = mysql_query("SELECT * FROM a WHERE 1 = 0");
    
    print_r($result); // => true
    
    ?>
    

    so if you get FALSE you can use

    mysql_errorno() and mysql_error() to know what happened..

    following with this:

    you can use mysql_fetch_array() to get row by row from a query

    0 讨论(0)
  • 2021-02-05 17:46

    I used the following:

    if ($result != 0 && mysql_num_rows($result)) {

    If a query returns nothing it will be a boolean result and it's value will be 0.

    So you check if it's a zero or not, and if not, we know there's something in there..

    HOWEVER, sometimes it'll return a 1, even when there is nothing in there, so you THEN check if there are any rows and if there is a full row in there, you know for sure that a result has been returned.

    0 讨论(0)
提交回复
热议问题