简体   繁体   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

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

Here's the code 这是代码

$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)){

so when im logged in user id=2 user id=1 posts are shown 因此,当我登录时,用户ID = 2的用户ID = 1帖子显示

You need to specify the user id in your query, why you are getting images for user 1 is that your code at the moment runs as expected. 您需要在查询中指定用户ID,为什么要获得用户1的图像是因为您的代码目前正在按预期运行。

If you want to get data only for user1, you need to add a where clause specifying the user ID. 如果只想获取user1的数据,则需要添加where子句以指定用户ID。

Like so: 像这样:

//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'";

EDIT: Actually I just noticed that @04FS already mentioned it in a comment. 编辑:实际上我只是注意到@ 04FS已经在评论中提到了它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当table1.id = table2.id时如何打印一件事,而如果“ if else”又如何打印? - How can I print one thing when table1.id=table2.id, and another thing 'if else'? PHP MYSQL JOIN QUERY 2 数据库,其中 table1.id = table2.id,如何将 table2.content 显示为 table1.id - PHP MYSQL JOIN QUERY 2 Databases, Where table1.id = table2.id, How to display table2.content as table1.id MySQL Select语句where table1.id!= table2.id - MySQL Select statement Where table1.id != table2.id SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE子句 - SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE clause MySQL使用table1.id REGEXP table2.id为同一组选择不同的行 - MySQL select distinct rows for same group using table1.id REGEXP table2.id Laravel 一页杂物 Select 全部来自 table2 其中 id = table1.id - Laravel one page crud Select all from table2 where id = table1.id PHP MYSQL - 突出显示数据库表行IF table1.id存在于table2中 - PHP MYSQL - Highlight a database table row IF table1.id exists in table2 连接两个表,其中table1.id等于table2.table1_id,但仅显示table1.id中在table2.table1_id中找不到的行 - Join two tables where table1.id equals table2.table1_id, but only display rows from table1.id that cannot be found in table2.table1_id 两个php查询,显示表1的所有结果,标记为id,表2中的用户相同 - two php querys, show all results from table 1 and mark were id and user is the same in table 2 Mysql - UPDATE表SET列= SELECT COUNT(*)FROM(SELECT * FROM table2 WHERE table2.id = table.id))不可能 - Mysql — UPDATE table SET column = SELECT COUNT(*) FROM ( SELECT * FROM table2 WHERE table2.id = table.id ) ) Impossible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM