尝试显示来自数据库的消息,但它们不会显示

时间:2014-11-01 09:49:04

标签: php mysql

我遇到了一个问题,因为我正在测试一个消息传递程序,但它没有用。 我想展示用户的谈话,但是evreything仍然是空白的。 这是代码:

<?php
include_once('config.php');
include_once('functions.php');
$user_one = "13";
$db = safe_db_open($host, $gebruiker, $wachtwoord, $database);
$query= ("SELECT u.user_id,c.c_id,u.username,u.email
 FROM conversation c, users u
 WHERE CASE 
 WHEN c.user_one = '$user_one'
 THEN c.user_two = u.user_id
 WHEN c.user_two = '$user_one'
 THEN c.user_one= u.user_id
 END 
 AND (
 c.user_one ='$user_one'
 OR c.user_two ='$user_one'
 )
 Order by c.c_id DESC Limit 20");

$result = safe_query($db, $query);

while($row = mysqli_fetch_array($result))
{
 $c_id=$row['cid'];
 $user_id=$row['user_id'];
 $username=$row['username'];
 $email=$row['email'];
 $cquery= ("SELECT R.cr_id,R.time,R.reply FROM conversation_reply R WHERE R.c_id_fk='$c_id' ORDER BY R.cr_id DESC LIMIT 1");
 $cresult = safe_query($db, $cquery);
 $crow= mysqli_fetch_array($cresult);
 $cr_id=$crow['cr_id'];
 $reply=$crow['reply'];
 $time=$crow['time']; 
 echo "$time : $reply";
}

1 个答案:

答案 0 :(得分:0)

试试这个:

<?php
include_once('config.php');
include_once('functions.php');
$user_one = "13";
$db = safe_db_open($host, $gebruiker, $wachtwoord, $database);
$query= ("SELECT u.user_id, c.c_id, u.username, u.email
 FROM conversation c, users u
 WHERE CASE 
 WHEN c.user_one = '{$user_one}'
 THEN c.user_two = u.user_id
 WHEN c.user_two = '{$user_one}'
 THEN c.user_one = u.user_id
 END 
 AND (
 c.user_one = '{$user_one}'
 OR c.user_two ='{$user_one}'
 )
 ORDER BY c.c_id DESC LIMIT 20");

$result = safe_query($db, $query);

while($row = mysqli_fetch_array($result))
{
 $c_id = $row[cid];
 $user_id = $row[user_id];
 $username = $row[username];
 $email = $row[email];
 $cquery = ("SELECT R.cr_id, R.time, R.reply FROM conversation_reply R WHERE R.c_id_fk='{$c_id}' ORDER BY R.cr_id DESC LIMIT 1");
 $cresult = safe_query($db, $cquery);
 $crow = mysqli_fetch_array($cresult);
 $cr_id = $crow[cr_id];
 $reply = $crow[reply];
 $time = $crow[time]; 
 echo "$time : $reply";
}
?>

<强> BUT: $ cquery中的R是什么? 如果它应该是&#34; conversation_reply R&#34;比如DB中的单元格名称,请尝试执行以下操作:

在include functions.php之后添加:

$helpvar = "conversation_reply R";

然后将$ cquery更改为:

$cquery= ("SELECT R.cr_id, R.time, R.reply FROM '{$helpvar}' WHERE R.c_id_fk='$c_id' ORDER BY R.cr_id DESC LIMIT 1");