简体   繁体   English

检查mysql_query()是否返回任何结果的正确方法是什么?

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

I tried what seemed like the most intuitive approach 我尝试了看起来最直观的方法

$query = "SELECT * FROM members 
          WHERE username = '$_CLEAN[username]'
          AND password = '$_CLEAN[password]'";
$result = mysql_query($query);

if ($result)
{ ... 

but that didn't work because mysql_query returns a true value even if 0 rows are returned. 但这不起作用,因为即使返回0行, mysql_query也会返回一个真值。

I basically want to perform the logic in that condition only if a row is returned. 我基本上只想在返回行的情况下执行该逻辑。

Use mysql_num_rows : 使用mysql_num_rows

 if (mysql_num_rows($result)) {
    //do stuff
 }

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. 您可以改用mysql_fetch_array()或其他方法,但原理相同。 If you're doing expecting 1 or more rows: 如果您要期待1行或更多行:

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

This will loop until it runs out of rows, at which point it'll continue on. 这将循环直到行用完为止,然后继续。

mysql_num_rows

Retrieves the number of rows from a result set. 从结果集中检索行数。 This command is only valid for statements like SELECT or SHOW that return an actual result set. 该命令仅对返回实际结果集的SELECT或SHOW语句有效。

If none match, then zero will be the return value and effectively FALSE . 如果不匹配,则返回值为零,有效值为FALSE

$result = mysql_query($query);

if(mysql_num_rows($result))
{ //-- non-empty rows found fitting your SQL query

  while($row = mysql_fetch_array($result))
  {//-- loop through the rows, 
   //--   each time resetting an array, $row, with the values

  }
}

Which is all good and fine if you only pull out of the database. 如果您仅退出数据库,那一切都很好。 If you change or delete rows from the database and want to know how many were affected by it... 如果您更改或删除数据库中的行,并想知道有多少行受其影响...

To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows() . 要检索受INSERT,UPDATE,REPLACE或DELETE查询影响的行数,请使用mysql_affected_rows()

$result = mysql_query($query);

if(mysql_affected_rows())
{ //-- database has been changed 

}

//-- if you want to know how many rows were affected:
echo 'Rows affected by last SQL query: ' .mysql_affected_rows();

mysql_query() will only return FALSE if the query failed. 如果查询失败, mysql_query()将仅返回FALSE It will return TRUE even if you have no rows, but successfully queried the database. 即使没有行,但查询数据库成功,它也会返回TRUE

$sql = "SELECT columns FROM table";
$results = mysql_query($sql, $conn);
$nResults = mysql_num_rows($results);
if ($nResults > 0) {
   //Hurray
} else {
   //Nah
}

This should work. 这应该工作。

I used the following: 我使用了以下内容:

if ($result != 0 && mysql_num_rows($result)) { 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. 如果查询不返回任何内容,则将是布尔结果,其值为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. 但是,有时即使其中没​​有任何内容,它也会返回1,因此您然后检查其中是否有任何行以及其中是否有完整的行,就可以确定已经返回了结果。

What about this way: 那么这种方式呢:

$query = "SELECT * FROM members WHERE username = '$_CLEAN[username]'
                                  AND password = '$_CLEAN[password]'";
$result = mysql_query($query);
$result = mysql_fetch_array($result);

//you could then define your variables like:
$username = $result['username'];
$password = $result['password'];

if ($result)
{ ...

I like it because I get to be very specific with the results returned from the mysql_query. 我喜欢它,因为我对mysql_query返回的结果非常具体。

-Ivan Novak -伊万·诺瓦克(Ivan Novak)

well... 好...

by definiton mysql_query: 通过definiton mysql_query:

mysql_query() returns a resource on success, or FALSE on error. mysql_query()成功返回资源,错误返回FALSE。

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. 但是您需要了解的是,如果此函数返回的值与FALSE不同,则查询已成功运行(没有错误的语法,正确的连接,仍然有效,等等),但这并不意味着您的查询正在返回某行。

for example 例如

<?php

$result = mysql_query("SELECT * FROM a WHERE 1 = 0");

print_r($result); // => true

?>

so if you get FALSE you can use 因此,如果您获得FALSE,则可以使用

mysql_errorno() and mysql_error() to know what happened.. mysql_errorno()mysql_error()知道发生了什么。

following with this: 以下:

you can use mysql_fetch_array() to get row by row from a query 您可以使用mysql_fetch_array()从查询中逐行获取

$result = mysql_query(...);

if(false !== $result)
{
    //...
}

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

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