$query = "SELECT * FROM `table`";
$results = mysql_query($query, $connection);
如果'table'没有行。最简单的检查方法是什么?
发布于 2019-06-20 09:10:33
Jeremy Ruten上面的答案很好并且执行得很快; 另一方面,它只给你行数而没有别的(如果你想要结果数据,你必须再次查询数据库)。我用的是什么:
// only ask for the columns that interest you (SELECT * can slow down the query)
$query = "SELECT some_column, some_other_column, yet_another_column FROM `table`";
$results = mysql_query($query, $connection);
$numResults = mysql_num_rows($results);
if ($numResults > 0) {
// there are some results, retrieve them normally (e.g. with mysql_fetch_assoc())
} else {
// no data from query, react accordingly
}
发布于 2019-06-20 10:03:14
我注意到遗漏的查询可能不成功,所以你需要检查是否设置了$ results变量。我将以yjerem给出的答案为例。
$query = "SELECT COUNT(*) AS total FROM table";
$results = mysql_query($query, $connection);
if ($results) { // or use isset($results)
$values = mysql_fetch_assoc($results);
$num_rows = $values['total'];
}
https://stackoverflow.com/questions/-100001256
复制相似问题