我写了这个查询以获取详细信息,但它不起作用。这是我的代码:
$sender_id=$_GET['sender_id'];
$sender_id=mysqli_real_escape_string($con,$sender_id);
$sender_id=htmlentities($sender_id);
$sql1="SELECT * FROM massges where receiver_id='".$_SESSION['u_id']."' and sender_id='".$sender_id."' order by msg_id";
$sql2="SELECT * FROM massges where receiver_id='".$sender_id."' and sender_id='".$_SESSION['u_id']."' order by msg_id";
$sqls=[$sql1,$sql2];
foreach($sqls as $k => $sql){
$run=mysqli_query($con,$sql);
if (mysqli_num_rows($run)>0) {
while ($row=mysqli_fetch_assoc($run)) {
$msg_text=$row['msg_text'];
$msg_id=$row['msg_id'];
$query1="SELECT * FROM users where u_id='".$sender_id."'";
$query2="SELECT * FROM users where u_id='".$_SESSION['u_id']."'";
$querys=[$query1,$query2];
foreach($querys as $k => $query){
$res=mysqli_query($con,$query);
while($data=mysqli_fetch_assoc($res)){
$user_image=$data['user_image'];
}
echo '<br>
<div class="row">
<div class="col s2" style="margin: 0px;">
<img src="users/'.$user_image.'" class="circle right" height="20px">
</div>
<div class="col s6"><span class=" blue lighten white-text" style="margin: 0px;border-radius: 10px;padding:5px;">'.$msg_text.'<span></div>
</div>';}
以下是显示的数据: Result Image
如何显示发送者和接收者的消息?
答案 0 :(得分:0)
您可以在一个查询中执行此操作,如果这样做,您的代码将更容易调试:
$sender_id = $_GET['sender_id'];
$user_id = $_SESSION['u_id'];
// Get all Messages where the user and sender are involved
$statement = $con->prepare('
SELECT
messages.msg_text,
users.user_image
FROM
messages
JOIN
users ON users.u_id = messages.sender_id
WHERE
(
messages.sender_id = ?
AND messages.receiver_id = ?
) OR (
messages.sender_id = ?
AND messages.receiver_id = ?
)
ORDER BY
messages.msg_id
');
$statement->bind_param(
"iiii",
$sender_id,
$user_id,
$user_id,
$sender_id
);
$statement->execute();
// Assign the content of 'msg_text' and 'user_image'
$statement->bind_result($text, $image);
// loop through all results
while ($statement->fetch()) {
/* ECHO YOUR HTML HERE */
echo $text;
echo $image;
}
可能有用的链接: http://php.net/manual/en/mysqli-stmt.bind-param.php http://php.net/manual/en/mysqli-stmt.bind-result.php
答案 1 :(得分:0)
$sender_id = $_GET['sender_id'];
$user_id = $_SESSION['u_id'];
$sql="SELECT
massges.msg_text,
massges.msg_id,
users.user_image
FROM
massges
JOIN
users ON users.u_id = massges.sender_id
WHERE
(
massges.sender_id = '".$sender_id."'
AND massges.receiver_id = '".$user_id."'
) OR (
massges.sender_id = '".$user_id."'
AND massges.receiver_id = '".$sender_id."'
)
ORDER BY
massges.msg_id";
$run=mysqli_query($con,$sql);
if (mysqli_num_rows($run)>0) {
while ($row=mysqli_fetch_assoc($run)) {
$msg_id=$row['msg_id'];
$msg_text=$row['msg_text'];
$user_image=$row['user_image'];
echo '<br>
<div class="row">
<div class="col s2" style="margin: 0px;">
<img src="users/'.$user_image.'" class="circle right" height="20px">
</div>
<div class="col s6"><span class=" blue lighten white-text" style="margin: 0px;border-radius: 10px;padding:5px;">'.$msg_text.'<span></div>
</div>'; } }