【问题标题】:PHP SQL query in while loop JOIN在while循环JOIN中的PHP SQL查询
【发布时间】:2013-10-27 13:40:00
【问题描述】:

我正在为一个练习项目制作一个基本的通知系统。我有几个表,其中两个如下所示:

> Table 1: "Users"
> 
userid | username  | Firstname | Lastname  
1      | Daffy     |   Daffy   | Duck
2      | Flinstone |   Fred    | Flinstone 
3      | dduck     |   Donald  | Duck
> 
> Table 2: "Notifications"
> 
 Notification_id | from_user_id | to_user_id | SeenOrUnseen | type    
     1           |   1          |     2      |       1      |  fRequest               
>    2           |   1          |     2      |       0      |  comment               
>    3           |   1          |     2      |       1      |  comment               
>    4           |   3          |     1      |       1      |  fRequest               
>    5           |   2          |     3      |       1      |  fRequest               
>    6           |   2          |     3      |       0      |  comment               
>    7           |   3          |     2      |       0      |  comment

然后我需要这两个表中的数据,并且通常会在发送 sql 查询之前加入 user_id 和 from_user_id 上的表。但是,连接似乎返回多个值,因为在第二个表中有多个 from_user_id 实例。相反,我正在查询数据库,在 while 循环中返回数据,并在该 while 循环中向数据库发送另一个查询以获取不同表的信息:

include('../../db_login.php');
$con = mysqli_connect("$host", "$username", "$password", "$db_name");
$tbl_name = "Profile";
$tplname = "profile_template.php";
$tplname2 = "public_profile_template.php";
$myid = $_SESSION['identification'];
//CHECK CONNECTION
if(mysqli_connect_errno($con))  {
echo "failed to connect" . mysql_connect_error();
}else {
$result = mysqli_query($con, "SELECT * FROM Notifications WHERE to_user_id='$myid'");
$count = mysqli_num_rows($result);
if($count == 0){

    $style = "display:none;";
} else {

echo "<ul class='notifications'>";
    while($row = mysqli_fetch_array($result)){
    $from_user_id = $row['from_user_id'];
    $to_user_id = $row['to_user_id'];
    $seen = $row['seen'];
    $nature = $row['nature'];
        $result2 = mysqli_query($con, "SELECT * FROM users WHERE id='$from_user_id'");
        $count2 = mysqli_num_rows($result2);
        if($count2 != 0){
        while($row2 = mysqli_fetch_array($result2)){
        $fromFirstname = $row2['Firstname'];
        $fromLastname = $row2['Lastname'];

        }
    if($nature == 'fRequest'){
    echo "<li> You have received a friend request from" . $fromFirstname . " " . $fromLastname . "</li>";
    }
    }

    }   
    echo "</ul>";
}


mysqli_close($con);
}
                echo "<div id='NoNotification'></div>";
                echo "<div id='Notification' style='" . $style . "'></div>";
                ?>

有更好的方法吗?

感谢您的帮助!

【问题讨论】:

  • 是的。重写为连接。您说它不起作用,但这仅意味着您没有正确编写联接。像这样的嵌套查询,其中内部查询依赖于外部查询的值,效率非常低。
  • 是的。这样做有更好的办法。如果您只想要 DISTINCT 结果,请仅选择 DISTINCT 结果。
  • 酷,这就是我认为的马克 B!有另一个加入,但下面的答案几乎是我以前做过的。在这种情况下,最好的 Join 是什么?
  • 不用担心,现在可以使用了。我的坏!

标签: php mysql sql join while-loop


【解决方案1】:

你可以这样做:

SELECT n.*, u.*
FROM Notifications n 
JOIN users u ON n.from_user_id=u.id
WHERE n.to_user_id=$myid
ORDER BY n.id, u.id

您将获得所需的所有数据。通知数据和用户数据。最好定义您要检索的字段而不是 *,这样您就可以更好地了解您使用的内容以及不发送不使用的数据。

如果您使用整数 (id) 作为字符串,您可以将 $myid 放在那里。还要注意这不是 MySQL 注入的安全查询。 For more info.

【讨论】:

    猜你喜欢
    • 2014-10-18
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 2013-03-11
    • 2011-10-11
    相关资源
    最近更新 更多