我正在尝试建立一个个人资料页面,您可以在其中查看您的朋友关系,每个朋友"显示个人资料图片,用户名,姓名和姓氏,"朋友的数量"和帖子数量。我使用ajax get获取结果。我能在phpmyadmin中使用结果的唯一方法是通过2个select语句。在我的php中运行它时,我无法使用关联数组来处理结果。这是我的代码:
$json = '{"people":[';
$sql = "SELECT u.uUsername, u.uName, u.uSurname, u.uProfilePic, u.User_ID, COUNT(c.Conn_ID) AS Conns
FROM tbusers u INNER JOIN tbconnections c
ON u.User_ID = c.connected OR u.User_ID = c.connector
WHERE c.connector = $user OR c.connected = $user
GROUP BY u.User_ID;
SELECT COUNT(n.Post_ID) AS Posts
FROM tbusers u INNER JOIN tbconnections c
ON u.User_ID = c.connected OR u.User_ID = c.connector
INNER JOIN tbposts n
ON n.User_ID = u.User_ID
WHERE c.connector = $user OR c.connected = $user
GROUP BY u.User_ID";
if($conn->multi_query($sql)) {
do {
if($result = $conn->store_result()) {
$res = $result->fetch_all(MYSQLI_ASSOC);
foreach ($res as $row){ var_dump($row) ;
$json .= '{"username" : "'.$row['uUsername'].'", "name" : "'.$row['uName'].'", "surname" : "'.$row['uSurname'].'",'.
' "pic" : "'.$row['uProfilePic'].'", "uid" : "'.$row['User_ID'].'", "conns" : "'.$row['Conns'].'"},';
}
}
}
while($conn->more_results() && $conn->next_result());
$json = substr($json, 0, strlen($json) - 1);
$json .= ']}';
echo htmlspecialchars($json, ENT_NOQUOTES);
我从var_dump得到的结果是:
array(6){[" uUsername"] =>字符串(11)" Einsteinium" [" UNAME"] => string(5)" Charl" [" uSurname"] => string(6)" Meyers" [" uProfilePic"] => string(24)" images / profilePics / 1.jpg" [" USER_ID"] =>字符串(1)" 1" ["&是conns#34;] =>字符串(1)" 1" } array(6){[" uUsername"] => string(5)" Pinky" [" UNAME"] => string(7)" Mariska" [" uSurname"] => string(6)" Meyers" [" uProfilePic"] => string(24)" images / profilePics / 4.jpg" [" USER_ID"] =>字符串(1)" 4" ["&是conns#34;] =>字符串(1)" 2" } array(6){[" uUsername"] =>字符串(6)" Sannie" [" UNAME"] => NULL [" uSurname"] => NULL [" uProfilePic"] => NULL [" User_ID"] =>字符串(1)" 5" ["&是conns#34;] =>字符串(1)" 1" } array(1){["帖子"] =>字符串(1)" 6" }
但是我在运行时仍然会收到以下注意事项,请注意即使我使用正确的索引它也会显示未定义的索引:
注意:未定义的索引:第182行的C:\ xampp \ htdocs \ IMY \ Project \ profileChange.php中的uUsername
注意:未定义的索引:第182行的C:\ xampp \ htdocs \ IMY \ Project \ profileChange.php中的uName
注意:未定义的索引:第182行的C:\ xampp \ htdocs \ IMY \ Project \ profileChange.php中的uSurname
注意:未定义的索引:第183行的C:\ xampp \ htdocs \ IMY \ Project \ profileChange.php中的uProfilePic
注意:未定义的索引:第183行的C:\ xampp \ htdocs \ IMY \ Project \ profileChange.php中的User_ID
注意:未定义的索引:第183行的C:\ xampp \ htdocs \ IMY \ Project \ profileChange.php中的Conns
它似乎也在var_dump($row)
输出第二个选择语句:
array(1){["帖子"] =>字符串(1)" 1" }
json字符串确实包含我想要的数据,但其中包含3个额外的结果:
{"人":[{"用户名" :" Einsteinium"," name" :" Charl","姓" :" Meyers"," pic" :" images / profilePics / 1.jpg"," uid" :" 1"," conns" :" 1"},{"用户名" :" Pinky"," name" :" Mariska","姓" :" Meyers"," pic" :" images / profilePics / 4.jpg"," uid" :" 4"," conns" :" 2"},{"用户名" :" Sannie"," name" :"","姓氏" :""," pic" :""," uid" :" 5"," conns" :" 1"},{"用户名" :""," name" :"","姓氏" :""," pic" :""," uid" :""," conns" :""},{"用户名" :""," name" :"","姓氏" :""," pic" :""," uid" :""," conns" :""},{"用户名" :""," name" :"","姓氏" :""," pic" :""," uid" :""," conns" :""}]}
我知道我做得不对,做这件事的方法是什么?