duanhan4763 2016-06-29 19:11
浏览 56

即使表中存在值DO,mysql查询返回false? 试着找不到表?

Ok, so I am building on my first question here https://stackoverflow.com/questions/38102208/php-mysql-how-to-only-echo-links-with-search-bar-post-that-arent-already-echo

trying to only echo only usernames of people whose id is NOT in a mysql table called conversation along with a set id (the person who is signed in).

I echo the people who their id is user_two in a table conversation REGARDLESS if a search bar is posted here:

//$num = mysqli_query($con, "SELECT * FROM `pm_messages` WHERE user_from=".$account['id']."");
$numCon = mysqli_query($con, "SELECT * FROM `conversation` WHERE user_one=".$account['id']."");
$numrows = mysqli_num_rows($numCon);

while ($u = mysqli_fetch_assoc($numCon)) {
    //get other users usernames to echo link
    $getUserTwo = mysqli_query($con, "SELECT * FROM `accounts` WHERE id=".$u['user_two']."");
    $s = mysqli_fetch_assoc($getUserTwo);

    //echo $s['username'];
    echo "<a href='message.php?id={$s['id']}'><li><img class = 'dmCircle' src = '../images/chatCircle.png'/>{$s['username']} </li></a>";
}

This works well, meaning only individuals who a conversation has been started with (a row exists for this user and the one signed in conversation table) are echoed in a link.

Problem comes here with the search bar because it echoes all individuals even if a conversation has been started, resulting in duplicates:

enter image description here

(notice the 2 khusteds)

This does not make sense because here I select the row in conversation where user_one is the signed in user and user_two is the second user and only echo a link if the result is FALSE (meaning there's no conversation):

if (isset($_POST['searchbarpm'])) {
    //$sess->getUsers();
    $dbh = mysqli_connect("localhost","username","password","sqlserver");
    $query = $_POST['searchbarpm'];
    $q = mysqli_query($dbh, "SELECT * FROM sqlserver.accounts WHERE username LIKE '%".$query."%'");

    //display all the results
    while($row = mysqli_fetch_assoc($q)) {

        $checkConvo = mysql_query("SELECT 'id' FROM sqlserver.conversation WHERE user_one=".$user_id." AND user_two=".$row['id']."");

        //only output users they dont have convo going with because theyre already printed!!!
        if ($checkConvo==false && $row['id']!= $user_id) { 
            echo "<a href='message.php?id={$row['id']}'><li><img class = 'dmCircle' src = '../images/noChatCircle.png'/> {$row['username']}</li></a>";
        }
    }
}

But it looks like the query is always false because again, all users are echoed. Why is this happening? How can I only echo users not in conversation table with the signed in user (user_one)? EDIT:

enter image description here new code (sorry for screenshot); :

enter image description here

@IanH -

$con = mysqli_connect("localhost","username","password","sqlserver");

                    //$num = mysqli_query($con, "SELECT * FROM `pm_messages` WHERE user_from=".$account['id']."");
                $numCon = mysqli_query($con, "SELECT * FROM `conversation` WHERE user_one=".$account['id']."");
                    $numrows = mysqli_num_rows($numCon);
                while ($u = mysqli_fetch_assoc($numCon))
                    {
                    //get other users usernames to echo link
    $getUserTwo = mysqli_query($con, "SELECT * FROM `accounts` WHERE id=".$u['user_two']."");
        $s = mysqli_fetch_assoc($getUserTwo);
                    //echo $s['username'];

                    if(isset($_POST['searchbarpm'])){
//$sess->getUsers();
    $dbh = mysqli_connect("localhost","username","password","sqlserver");
                    $query = $_POST['searchbarpm'];
                    $q = mysqli_query($dbh, "SELECT * FROM sqlserver.accounts WHERE username LIKE '%".$query."%'");
                    //display all the results
                    while($row = mysqli_fetch_assoc($q)){


                        if($row['id']!= $user_id && $row['id']!=$s['id']) { //only output users they dont have convo going with because theyre already printed!!!
                        echo "<a href='message.php?id={$row['id']}'><li><img class = 'dmCircle' src = '../images/noChatCircle.png'/> {$row['username']}</li></a>";
                        }
                    }
}
                    else {


                    echo "<a href='message.php?id={$s['id']}'><li><img class = 'dmCircle' src = '../images/chatCircle.png'/>{$s['username']} </li></a>";
                    }
                }//
  • 写回答

1条回答 默认 最新

  • doujiu7704 2016-06-29 19:19
    关注

    First of all, you need change the mysql_query( ... ) on line 9 of your second posted code block to mysqli_query( ... ), for API consistency and compatibility.

    Also, you could have duplicate results if you are allowing multiple entries in the conversation table where users A and B can be entered as user1 = A, user2 = B in one conversation, and user1 = B, user2 = A in a different conversation.

    Lastly, as Jay Blanchard said, you should use prepared statements to avoid SQL injection.

    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私