[英]Not getting a result from DB
So I'm checking to see if a user already liked a post. 因此,我正在检查用户是否已喜欢该帖子。 Here's what I'm doing
这就是我在做什么
$id = 65;
//Get likes count
$stmt = $con->prepare("SELECT * FROM likes WHERE liked_post_id = :liked_post_id");
$stmt->bindValue(':liked_post_id', $id, PDO::PARAM_STR);
$stmt->execute();
$return = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<pre>
<?php
print_r($return);
?>
</pre>
<?php
//Get user IP
$ip = $_SERVER['SERVER_ADDR'];
//Check if user liked post
$result = $con->prepare("SELECT * FROM likes WHERE liked_post_user = :username");
$result->bindParam(':username', $_SESSION['user']);
$result->execute();
$reprint = $result->fetch(PDO::FETCH_ASSOC);
echo $reprint['liked_post_user'];
$return_cnt = count($reprint);
if($return_cnt < 1){
//Insert like
$query = $con->prepare("INSERT INTO likes (liked_post_id, liked_post_user, liked_post_ip) VALUES (:usr_id, :user, :ip)");
$query->bindValue(':usr_id', $id, PDO::PARAM_STR);
$query->bindValue(':user', $_SESSION['user']);
$query->bindValue(':ip', $ip, PDO::PARAM_STR);
$query->execute();
}
The problem is $query
never gets ran. 问题是
$query
永远不会运行。 Even though I have no record of the username in the DB. 即使我没有数据库中用户名的记录。 So I'd expect it to run once, and insert
$query
into the DB, once. 因此,我希望它可以运行一次,然后将
$query
一次插入数据库。 But it isn't. 但事实并非如此。 I'm not getting any errors either.
我也没有任何错误。 Any ideas?
有任何想法吗?
The second query should look for a specific liked_post_id
. 第二个查询应查找特定的
liked_post_id
。 It's currently looking for any posts that the user liked, not just this one. 目前,它正在寻找用户喜欢的任何帖子,而不仅仅是此帖子。
//Check if user liked post
$result = $con->prepare("SELECT * FROM likes WHERE liked_post_user = :username AND liked_post_id = :id");
$result->bindParam(':username', $_SESSION['user']);
$result->bindParam(':id', $id);
$result->execute();
$reprint = $result->fetch(PDO::FETCH_ASSOC);
And then you should test whether the query found anything by testing whether $reprint
is an array or false
: 然后,您应该通过测试
$reprint
是数组还是false
来测试查询是否找到了任何东西:
if ($reprint) {
echo $reprint['liked_post_user'];
} else {
//Insert like
$query = $con->prepare("INSERT INTO likes (liked_post_id, liked_post_user, liked_post_ip) VALUES (:usr_id, :user, :ip)");
$query->bindValue(':usr_id', $id, PDO::PARAM_STR);
$query->bindValue(':user', $_SESSION['user']);
$query->bindValue(':ip', $ip, PDO::PARAM_STR);
$query->execute();
}
First of all you can simply count $reprint
. 首先,您可以简单地计算
$reprint
。 To answer your second issue, you need to select the post, or else it'll simply check all the posts. 要回答第二个问题,您需要选择帖子,否则它将仅检查所有帖子。 So do
也是
$result = $con->prepare("SELECT * FROM likes WHERE liked_post_user = :username AND liked_post_id = :post_id");
$result->bindParam(':username', $_SESSION['user']);
$result->bindParam(':post_id', $_GET['id']);
$result->execute();
$reprint = $result->fetch(PDO::FETCH_ASSOC);
Note that if no user is logged in it'll still input an empty value. 请注意,如果没有用户登录,它仍将输入一个空值。 So make sure to figure a way around that.
因此,请务必找出解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.