我有一个包含以下代码的 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);
?>
最佳答案
您正在寻找:
if(mysql_num_rows($result) == 0){
echo "9999";
} else {
文档:http://www.php.net/manual/en/function.mysql-num-rows.php
当你说 $result = mysql_query
时,你给 $result 赋了一个值,所以即使没有找到任何行,变量现在也被设置了,所以 isset()
赢了'帮助。
但是你真的不应该使用 mysql_* 函数。请注意准备好的语句。 PDO 或 mysqli。进一步阅读可以在这里找到:http://www.php.net/manual/en/function.mysql-query.php (红框)
关于php - 如何更改 !isset 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16094409/