MySQL COUNT 在 PHP 中不返回 False 或 True [英] MySQL COUNT not returning False or True in PHP
问题描述
所以我正在尝试检查用户是否已经喜欢了某个帖子.
So I'm trying to check if a user already liked a post.
代码:
function previously_liked($id) {
if (isset($_SESSION['user_login'])) {
$user = $_SESSION["user_login"];
}
else {
$user = "";
}
$connection = new mysqli($host, $username, $password, $database);
$id = $connection->real_escape_string($id);
$query = $connection->query("SELECT COUNT(`id`) AS `count` FROM `likes` WHERE `id` = '$id' AND `user_id` = '".$connection->escape_string($user)."'");
while ( $row = $query->fetch_object()->count ) {
if ( $row->count == 0 ) return false;
else return true;
}
}
问题是,它不会返回 false 或 true.在我的另一个 PHP 页面上,我试图像这样运行这个函数:
The thing is, it's not returning false or true. On my other PHP page I'm trying to run this function like this:
if (previously_liked(70) === true) {
$aliked = "You've already liked this!";
}
echo $aliked;
我看到的唯一错误是 Notice: Undefined variable: aliked in profile.php on line 391
欢迎任何帮助!谢谢!
推荐答案
你可以这样做来发现代码中的错误:
You can do this things to finds error in your code:
echo $user; //before MySQL query
在替换.$connection->escape_string($user)"后,在 MySQL 控制台或 PHPmyAdmin 等应用程序中查询您的 MySQL 语句.到您的 user_id.如果它获取的行比您的 MySQL 语句正确,并且 $_SESSION["user_login"] 值有问题.
Query your MySQL statement in MySQL console or app like PHPmyAdmin after replacing ".$connection->escape_string($user)." to your user_id. If it fetch row than your MySQL statement is correct and there is something wrong with $_SESSION["user_login"] value.
可选地,您还可以回显返回的行以检查输出数据.
Optionally, you can also echo the returning rows to check the output data.
否则检查您的查询语句,如列名、表名、数据库等
Otherwise check your query statement as column names, table name, database etc.
希望对你有帮助...
这篇关于MySQL COUNT 在 PHP 中不返回 False 或 True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!