gpt4 book ai didi

php - mysqli_multi_query() 重复获取 null

转载 作者:行者123 更新时间:2023-11-29 07:32:59 26 4
gpt4 key购买 nike

我正在尝试使用 mysqli_multi_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

最佳答案

合并您的查询以获得一个您可以随意排序的结果集:

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`;

我使用了 LEFT JOIN,但是如果所有用户在 ts_students_info 和 ts_students_info 中都有行,您可以将它们更改为仅 JOIN

编辑

要在下面的评论中回答您的问题,选择多项技能,请将 where 子句更改为类似以下内容:

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

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

关于php - mysqli_multi_query() 重复获取 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50310085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com