如何检查 MySQL 结果在 PHP 中是否返回空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33561397/
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
How to check if MySQL results returned empty in PHP?
提问by Dnyan
How to check MySQL results are empty or not. If MySQL query results are empty then else condition should not be executed. In case MySQL results in data there & in else condition my error my message is there but it is not showing any error message. I have tried the following code but not showing any alert or echo message on the screen.
如何检查 MySQL 结果是否为空。如果 MySQL 查询结果为空,则不应执行 else 条件。如果 MySQL 在那里产生数据,在其他条件下我的错误我的消息在那里,但它没有显示任何错误消息。我尝试了以下代码,但没有在屏幕上显示任何警报或回显消息。
<?php
$sql = "select * from hall_search_data_1 where rent BETWEEN '".$_SESSION['amount1']."' AND '".$_SESSION['amount2']."'";
$res = mysql_query($sql);
if (!empty($res)) {
while ($row = mysql_fetch_row($res)) {
// here my data
}
} else {
echo "no results found";
}
回答by Ananta Prasad
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "id: " . $row["id"] . " - Name: " . $row["firstname"] . " " . $row["lastname"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
回答by Super Model
Check number of rows
检查行数
$result = mysqli_query($conn, $sql);
$rowcount=mysqli_num_rows($result);
if($rowcount > 0){
echo "Number of rows = " . $rowcount;
}
else
{
echo "no record found";
}
回答by Er. Amit Joshi
Security TipFirst of all stop using the
mysql_*
functions because they are not secure for production and later versions has stopped support for this API. So if accidentally you used those function in production then you can be in trouble.It is not recommended to use the old mysql extension for new development, as it was deprecated in PHP 5.5.0 and was removed in PHP 7. A detailed feature comparison matrix is provided below. More Read
安全提示首先停止使用这些
mysql_*
函数,因为它们在生产环境中不安全,并且更高版本已停止支持此 API。因此,如果您不小心在生产中使用了这些功能,那么您可能会遇到麻烦。不建议在新开发中使用旧的 mysql 扩展,因为它在 PHP 5.5.0 中被弃用,并在 PHP 7 中被删除。下面提供了详细的功能比较矩阵。更多阅读
For your answer you have to only check no of rows
is zero or not
Read this Post at php documentationwith Example.
对于您的答案,您只需检查no of rows
是否为零,请阅读php 文档中的这篇文章和示例。
回答by Suyog
You can use mysql_num_rowsto get count of number of rows returned from query.
您可以使用mysql_num_rows来获取查询返回的行数。
if(mysqli_num_rows($res) > 0)
{
// rest of your stuff
}
else
{
echo "No records found.";
}
Note:mysql is deprecated instead use mysqlior PDOas seen above
回答by Dharman
mysql_*
API has been removed from PHP long time ago. To access the database you should use PDO. Checking if PDO has returned any results is actually pretty simple. Just fetch the results and if the array is empty then there was nothing returned from MySQL.
mysql_*
很久以前,API 已经从 PHP 中删除了。要访问数据库,您应该使用 PDO。检查 PDO 是否返回任何结果实际上非常简单。只需获取结果,如果数组为空,则 MySQL 不会返回任何内容。
$stmt = $pdo->prepare('SELECT * FROM hall_search_data_1 WHERE rent BETWEEN ? AND ?');
$stmt->execute([$_SESSION['amount1'], $_SESSION['amount2']]);
$records = $stmt->fetchAll();
if ($records) {
foreach ($records as $row) {
// your logic
}
} else {
echo 'No records found!';
}
There is also mysqli library and if you are stuck using it you have to do a little more work, but the idea is the same. Fetch all results and if nothing was fetched then it means MySQL returned no rows.
还有 mysqli 库,如果你坚持使用它,你必须做更多的工作,但想法是一样的。获取所有结果,如果没有获取任何结果,则意味着 MySQL 没有返回任何行。
$stmt = $mysqli->prepare('SELECT * FROM hall_search_data_1 WHERE rent BETWEEN ? AND ?');
$stmt->bind_param('ss', $_SESSION['amount1'], $_SESSION['amount2']);
$stmt->execute();
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
if ($records) {
foreach ($records as $row) {
// your logic
}
} else {
echo 'No records found!';
}
回答by Saty
You can use mysql_num_rows();
to check your query return rows or not
您可以mysql_num_rows();
用来检查您的查询返回行与否
$sql = "select * from hall_search_data_1 where rent BETWEEN '".$_SESSION['amount1']."' AND '".$_SESSION['amount2']."'";
$res = mysql_query($sql);
$rows=mysql_num_rows($res);
if($rows>0)
{
echo "data return from query";
}else{
echo "data not return";
}
Note:- mysql is deprecated instead use mysqli or PDO
注意:- 不推荐使用 mysql,而是使用 mysqli 或 PDO
回答by Manoj Salvi
you can just replace if(!empty($res))
with mysql_num_rows like this -
你可以if(!empty($res))
像这样用 mysql_num_rows替换-
$sql = "select * from hall_search_data_1 where rent BETWEEN '".$_SESSION['amount1']."' AND '".$_SESSION['amount2']."'";
$res = mysql_query($sql);
if($countrows = mysql_num_rows($res) >= 1){
while($row=mysql_fetch_row($res))
{
here my data
}
}else{
echo "no results found";
echo "<br>";
echo "<script>alert('No any result found..!!');</script>";
echo "no results found";
}