我通过php在mysql中选择了一些东西,这个命令返回了一些数组(这是正确的),但是当我把返回的select放在if条件中并询问它是否返回null时,php说它返回null(这是不正确的,因为它返回的是数组)
include '../db.php'; // my config
function select($command) {
global $db;
$sql = "".$command."";
$sqlDone = $db -> prepare($sql);
$sqlDone -> execute();
$data = $sqlDone -> fetchAll();
return $data;
}
$select = "SELECT likes.ID, likes.ID_user, likes.ID_post FROM likes WHERE likes.ID_user = '53' AND likes.ID_post = '2'"
if (select($select) == null) { // goes throw this
print_r(select($select)); // returns array
} else {
echo 'not null';
}
我试着用!是空的,它无论如何都不工作。我试图将具有相同值的select命令直接放在phpmyadmin中,它返回数组,所以我很困惑。你能帮我吗?
1条答案
按热度按时间dpiehjr41#
pdo的
fetchAll()
返回一个数组,如果没有结果,则返回一个空数组(不为null)。只是使用
empty()
```$return = select($select); //put this into a variable, because if you don't, you'll query the database twice and may get different results.
if (empty($return)) { // goes throw this
print_r($return); // returns array
} else {
echo 'not null';
}
$return = $db->prepare($select)->execute()->fetchAll();
$return = $db->all($select);
$select = "SELECT likes.ID, likes.ID_user, likes.ID_post FROM likes WHERE likes.ID_user = ? AND likes.ID_post = ?"
$return = $db->all($select, [$userid, $postid]);