如何更改!isset语句

Zan*_*der 3 php mysql

我有一个PHP页面,下面的代码.MySQL查询工作正常,但我尝试添加一个无效的IF语句.该if(!isset($result))语句应该捕获表只包含FUTURE日期时间值的情况.我显然没有正确使用它,或者我应该使用别的东西 - 比如if(empty())

<?php

include 'quantitytest_config.php';

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("grace59_countdown",$dbhandle) 
  or die("Could not select countdown");

//execute the SQL query and return records
$result = mysql_query(
    "SELECT items 
    FROM cases 
    WHERE datetime<=NOW()
    Limit 1 ");

// check if there are only future dates in Database
if(!isset($result)){
    echo "9999";
} else {

//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
   echo "Quantity:".$row{'items'}."<br>";
}
}
//close the connection
mysql_close($dbhandle);
?>
Run Code Online (Sandbox Code Playgroud)

jth*_*man 6

您正在寻找:

if(mysql_num_rows($result) == 0){
   echo "9999";
} else {
Run Code Online (Sandbox Code Playgroud)

文档:http://www.php.net/manual/en/function.mysql-num-rows.php

当你这么说时,你给$ result赋值$result = mysql_query,即使没有找到行,现在设置变量,所以isset()也无济于事.

但你真的不应该使用mysql_*函数.相反,请考虑预备语句.PDO或mysqli.进一步阅读可以在这里找到:http://www.php.net/manual/en/function.mysql-query.php(红色框)

  • @BrandonWamboldt感谢您的澄清! (2认同)