簡體   English   中英

當我使用此php時,結果顯示所有表而不是table1.id = table2.id的特定條件

[英]When i use this php the results show all the table not the specific condition that table1.id = table2.id

  • 表格1
|---------------------|------------------|
|   User_id Primary   |      Email       |
|---------------------|------------------|
|         1           |      a@a.com     |
|---------------------|------------------|
|         2           |      b@b.com     |
|---------------------|------------------|
  • 表2
|---------------------|------------------|
|   Post_id Primary   |  User_id Foreign |
|---------------------|------------------|
|         1           |       1          |
|---------------------|------------------|

這是代碼

$conn1 = @mysqli_connect('127.0.0.1','root','','signup');

$sql1 = "SELECT post_pic ,post_text FROM table2 INNER JOIN table1
    ON (table2.User_id = table1.User_id) " ;

$result = mysqli_query($conn1, $sql1);

if(mysqli_num_rows($result)>0){
     while ($row = mysqli_fetch_array($result)){

因此,當我登錄時,用戶ID = 2的用戶ID = 1帖子顯示

您需要在查詢中指定用戶ID,為什么要獲得用戶1的圖像是因為您的代碼目前正在按預期運行。

如果只想獲取user1的數據,則需要添加where子句以指定用戶ID。

像這樣:

//For Arguments Sake
$user_id = $_SESSION['loggedin_user_id'];

$sql1 = "SELECT post_pic ,post_text
         FROM table2
         INNER JOIN table1
         ON (table2.id = table1.id)
         WHERE table2.User_id = '$user_id'";

編輯:實際上我只是注意到@ 04FS已經在評論中提到了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM