简体   繁体   English

php中mysql查询结果为null时如何打印

[英]how to print when the mysql query result is null in php

In MySQL query result is Null and how to display Null value in PHP. 在MySQL中,查询结果为Null,以及如何在PHP中显示Null值。 Below is the MySQL query 下面是MySQL查询

SELECT sum(qty),status,bs_id FROM booking_status where checkin <= '$date_search' and checkout='' and room_no='G 1'

I want to display message as no result found in PHP page 我想显示消息,因为在PHP页面中找不到结果

Thats not the best desciption, but if I understood you correctly, you have some query which returns an empty result. 那不是最好的决定,但是如果我对您的理解正确,那么您会有一些查询返回空结果。 Basically you need to display a message which says "Booking not found" or anything like that. 基本上,您需要显示一条消息,内容为“找不到预订”或类似的内容。

$result = /*some mysql null result*/; if ($result === null) { return "Booking not found"; }

Something like this? 像这样吗

$query = mysql_query("SELECT * FROM table");

if(!$query)
{
    echo "Null";
}

Base on what you need 根据您的需求

$query = mysql_query("SELECT sum(qty),status,bs_id FROM booking_status where checkin <= '$date_search' and checkout='' and room_no='G 1'");
if(!$query)
{
    echo "No results found.";
}
else
{
    // display the records here
}

Didn't try it. 没有尝试。 Don't know if it works. 不知道它是否有效。

There are two different possibilities you'll want to handle: 您需要处理两种不同的可能性:

  1. An error in the query, for whatever reason 查询错误,无论出于何种原因
  2. An empty result set 空结果集

Depending on which API you're using to build the query, it could look something like this: 根据您用来构建查询的API,它看起来可能像这样:

$query = $mysqli->query("SELECT ....");
if(!$query){ // problem in query
  echo "Invalid query";
}
if($query->num_rows == 0){ // empty result set
  echo "No results found";
}else{
  // do something with results
}

Try following. 请尝试以下。

$query = "SELECT sum(qty),status,bs_id FROM booking_status where checkin <= '$date_search' and checkout='' and room_no='G 1'";
$result = mysql_query( $query );

if( count($result) <=0 ) {
   echo "No result found.";
}

Thanks 谢谢

If you want a error free code then always do practice of handling errors in advance, whatever the result would be. 如果您想要一个没有错误的代码,那么无论结果如何,总是要事先练习处理错误。

  1. If you are writing any query then there will be chances of empty record or some other errors like(field not found, record is empty etc.) 如果您正在编写任何查询,那么将有可能出现记录为空或其他一些错误,例如(未找到字段,记录为空等)。
  2. What you have to do with this is always check for empty records like: 您必须始终检查空记录,例如:

    $sql = "SELECT sum(qty),status,bs_id FROM booking_status where checkin <= '".$date_search."' and checkout='' and room_no='G 1'"; $ sql =“ SELECT sum(qty),status,bs_id from booking_status,其中签入<='”。$ date_search。“'和checkout =” and room_no ='G 1'“;
    $sql_result = mysql_query($sql); $ sql_result = mysql_query($ sql);
    if(!$sql_result){ if(!$ sql_result){
    echo "Sorry no records found"; 回显“对不起,找不到记录”;
    }else{ }其他{
    $result = $sql_result->result_array(); $ result = $ sql_result-> result_array();
    } }

  3. By using this way your error get automatically handle. 通过这种方式,您的错误将自动得到处理。

use mysql_num_rows it gives you 0 when you get no result (no rows) from query. 使用mysql_num_rows ,当您从查询中获得任何结果(无行)时,它会给您0。 You can print anything when it equals 0 等于0时可以打印任何东西

$result = mysql_query("SELECT * FROM table1") or die(mysql_error());
$num_rows = mysql_num_rows($result);
if($num_rows == 0)
  echo "No Result Found";

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

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