隐藏所有被屏蔽朋友的评论

时间:2018-12-15 07:07:57

标签: php blocking

这是用于阻止来自被阻止用户的评论的代码。仅针对数据库表中第一个被阻止用户的评论被阻止,但我希望数据库表中所有被阻止用户的评论都被隐藏。

 <?php 
 include_once('adminpanel/dbconnect.php');
 $sql_query =mysql_query("SELECT * FROM blocked_accounts WHERE 
 blocker_id=".$id);
 $rr=mysql_fetch_array($sql_query);
 if($rr['blocked_id'] == $r['id'] && $rr['blocker_id'] == $id)
  {
echo "";    
  } 
 else
   {    ?>

2 个答案:

答案 0 :(得分:0)

您需要浏览所有记录,如果其中任何一个匹配,则该记录将被阻止。这段代码首先设置一个标志,表明它未被阻止,然后,如果有任何记录匹配,将其设置为true并退出循环(不值得继续执行)...

<?php
include_once('adminpanel/dbconnect.php');
$sql_query = $conn->prepare( "SELECT * FROM blocked_accounts WHERE
 blocker_id= ?");
$sql_query->bind_param("i", $id);
$sql_query->execute();
$blocked = false;
while ($rr=mysqli_fetch_assoc($sql_query))   {
    if($rr['blocked_id'] == $r['id'] && $rr['blocker_id'] == $id)
    {
        $blocked = true;
        break;
    }
}
if($blocked)  
{
    echo "";
}
else
{    ?>

如评论中所述,这是对mysqli_的更新和准备好的语句,您还需要更改连接以使用mysqli(如果不确定,PHP mysqli connect function可能会有所帮助)。 / p>

答案 1 :(得分:0)

使用mysql_*函数是非常不好的做法,因为它们已经过时很多年了。由于您处于学习阶段,因此最好专注于学习如何使用准备好的语句(无论是mysqli还是PDO的形式,并坚持使用。)

对于当前的问题,您提供的代码含糊不清,并且缺少发送查询所需的内容(例如连接)。因此,我的回答旨在指导您正确使用mysqli预准备语句,而不是提供全面的解决方案。

代码:

<?php
    # Establish a connection to the database.
    $connection = new mysqli("your host", "your username", "your password", "your db");

    # Create a mysqli query.
    $query = "SELECT * FROM `blocked_accounts` WHERE `blocker_id` = ?";

    # Prepare the query and check whether the operation was successful.
    if ($stmt = $connection -> prepare($query)) {
        # Bind the parameters to the statement.
        $stmt -> bind_param("i", $id);   # 'i' means integer

        # Execute the statement and check whether the operation was successful.
        if ($stmt -> execute()) {
            # Get the result out of the statement and cache it.
            $result = $stmt -> get_result();

            # Close the statement.
            $stmt -> close();

            # Fetch the first row (use 'while' if you want more).
            if ($row = $result -> fetch_assoc()) {
               # Check whether the user is blocked...
            }
        }
    }

    # Shut down the database connection.
    $connection -> close();
?>

注释:

  • 尝试查询数据库时,请记住使用已建立的实际连接。在您的mysql_query函数调用中,没有传递连接。
  • && $rr['blocker_id'] == $id检查中的if部分是多余的,因为$id的值是我们用来过滤数据库返回的结果的值,因此它将始终为{{ 1}}。