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

后端 未结 8 1337
佛祖请我去吃肉
佛祖请我去吃肉 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:30

    If you're checking for exactly one row:

    if ($Row = mysql_fetch_object($result)) {
        // do stuff
    }
    

    You can use mysql_fetch_array() instead, or whatever, but the principle is the same. If you're doing expecting 1 or more rows:

    while ($Row = mysql_fetch_object($result)) {
        // do stuff
    }
    

    This will loop until it runs out of rows, at which point it'll continue on.

提交回复
热议问题