显示从MySQL查询返回的记录

时间:2011-06-04 04:10:48

标签: php mysql

我无法显示发送给用户的消息。

假设用户是我的表“messages”中的id列,我将如何显示发送给该用户的消息?

session_start();
require "bidcon.php";
$userfinal=$_SESSION['username'];
$id = mysql_query("SELECT id FROM searchengine") or die(mysql_error());

// get the messages from the table.
$get_messages = mysql_query(
   "SELECT messages_id FROM messages
   WHERE to_user='$id' ORDER BY messages_id DESC")
  or die(mysql_error());

$get_messages2 = mysql_query(        
    "SELECT * FROM messages        
    WHERE to_user='$id' ORDER BY messages_id DESC")      
  or die(mysql_error());

$num_messages = mysql_num_rows($get_messages);
// display each message title, with a link to their content
echo '<ul>';

for($count = 1; $count <= $num_messages; $count++)
{   
    $row = mysql_fetch_array($get_messages2);

2 个答案:

答案 0 :(得分:0)

我不清楚你为什么需要两个不同的查询。但是你的主要问题是如何显示记录。您可以在while循环中执行此操作,如下所示:

$rs = mysql_query("SELECT id FROM searchengine") or die(mysql_error());
$rec = mysql_fetch_assoc($rs);
$id = $rec['id'];

// get the messages from the table.
$get_messages = mysql_query(
    "SELECT * FROM messages
    WHERE to_user='$id' ORDER BY messages_id DESC")
  or die(mysql_error());
$num_messages = mysql_num_rows($get_messages);

// display each message title, with a link to their content
echo '<ul>';
while($row=mysql_fetch_assoc($get_messages)){
    // do stuff with each row
    echo "<li>".$row['somefield']."</li>";
}
echo '</ul>';

答案 1 :(得分:0)

此:

$id = mysql_query("SELECT id FROM searchengine");

将使$id成为MySQL资源,而不是您正在寻找的整数/字符串。

请改为:

$res = mysql_query("SELECT id FROM searchengine");
$id = mysql_result($res, 0);