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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1180484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:23:30  来源:igfitidea点击:

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

phpmysql

提问by Ryan

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_queryreturns a true value even if 0 rows are returned.

但这不起作用,因为mysql_query即使返回 0 行也返回真值。

I basically want to perform the logic in that condition only if a row is returned.

我基本上只想在返回一行时才在该条件下执行逻辑。

回答by thedz

Use mysql_num_rows:

使用mysql_num_rows

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

回答by dirtside

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:

你可以mysql_fetch_array()改用,或者其他什么,但原理是一样的。如果您正在等待 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.

这将循环直到它用完行,此时它将继续。

回答by random

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.

mysql_num_rows

从结果集中检索行数。此命令仅对返回实际结果集的 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 FALSEif the query failed. It will return TRUEeven if you have no rows, but successfully queried the database.

mysql_query()只有FALSE在查询失败时才会返回。TRUE即使您没有行,它也会返回,但成功查询了数据库。

回答by Tebo

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

This should work.

这应该有效。

回答by Tebo

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,即使那里没有任何东西,所以你然后检查是否有任何行,如果那里有一整行,你肯定知道结果已经返回。

回答by Ivan Novak

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

-伊万诺瓦克

回答by Gabriel Sosa

well...

好...

by definiton mysql_query:

通过定义 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() 从查询中逐行获取

回答by joebert

$result = mysql_query(...);

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