Displaying Records Returned from MySQL Query
I can't display messages sent to a user.
Lets say that the user was the id column in my table "messages", how would I display messages sent to that user?
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_m开发者_JAVA百科essages);
// 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);
This:
$id = mysql_query("SELECT id FROM searchengine");
Will make $id
a MySQL resource, not the integer/string you're looking for.
Do this instead:
$res = mysql_query("SELECT id FROM searchengine");
$id = mysql_result($res, 0);
I'm not really clear on why you need two different queries. But your main question is around how to display the records. You would do it inside of the while loop, like so:
$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>';
精彩评论