mysqli\u multi\u query()用null重复获取的

mrwjdhj3  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(393)

我正在尝试使用mysqli\u multi\u query()从多个表中获取所有知道“photoshop”及其所有详细信息的用户。我也得到了结果,但是也有一些不速之客和正确的结果是空的。他们为什么在这里?
代码和输出如下:

<?php
    $sql = "SELECT user_id FROM ts_users_skills WHERE skillsTeach='Photoshop'";
    if($res = mysqli_query($conn,$sql)) {
        while ($id=mysqli_fetch_array($res)) {
                $id=$id['user_id'];
                // echo($id);
                $get_teachers_details="SELECT * FROM ts_students_log WHERE user_id='$id';";
                $get_teachers_details.="SELECT * FROM ts_students_info WHERE user_id='$id';";
       if (mysqli_multi_query($conn,$get_teachers_details))
        {
            do
                {
    // Store first result set
                if ($mul_res=mysqli_store_result($conn)) {
                  // Fetch one and one row
                  while ($row=mysqli_fetch_assoc($mul_res))
                    {
                    // printf("%s\n",$row['mobile']);
                         // print_r($row);
                        echo "Id: " . $row['user_id']. " Mobile: " . $row['mobile']. "Email:-" . $row['email'] . "Name:-" .$row['name'] . "<br/>";      
                    }           
                  //            Free result set         
                  mysqli_free_result($mul_res);         
                  }
                }
    while (mysqli_next_result($conn));
}
}
}
?>

输出:

Id: 3 Mobile: 7895447755Email:-aditya@chutiya.comName:-
     Id: 3 Mobile: Email:-Name:-Aditya Porwal
     Id: 4 Mobile: 9300010740Email:-rishabhgaandu@gmail.comName:-
     Id: 4 Mobile: Email:-Name:-Rishabh Agrawal
kr98yfug

kr98yfug1#

组合查询以获得单个结果集,您可以按自己的喜好进行排序:

SELECT 
    a.`user_id`,
    b.*,
    c.*
FROM ts_users_skills a
LEFT JOIN ts_students_log b
    ON a.`user_id` = b.`user_id`
LEFT JOIN ts_students_info c
    ON a.`user_id` = c.`user_id`
WHERE a.`skillsTeach` = 'Photoshop'
ORDER BY a.`user_id`;

我使用了左联接,但是如果所有用户都在ts\u students\u info和ts\u students\u info中都有行,您可以将它们更改为just joins。
编辑
要回答下面评论中的问题,选择多种技能,请将where子句改为:

WHERE  a.`skillsTeach` in ('Photoshop','Video Editing','Web Design')

WHERE  a.`skillsTeach` = 'Photoshop'
    OR a.`skillsTeach` = 'Video Editing'
    OR a.`skillsTeach` = 'Web Design'

相关问题