Jump to content

[SOLVED] Help Retrieving info from mysql


graham23s

Recommended Posts

Hi Guys,

 

i have made a very basic forum, i was going to make the posters names linkable to thier profiles but i'm having trouble with a query

 

here it is:

 

<?php
     // Query the second forum table...//////////////////////////////////////////////////
     $sql_2 = "SELECT * FROM `forum_replies` WHERE main_id='$id'";
     $result_2 = mysql_query($sql_2);  

     while($rows = mysql_fetch_array($result_2)) {
     
     $reply_id = $rows['reply_id']; 
     $reply_message = $rows['reply_message'];
     
     // ANOTHER query to get the id...///////////////////////////////////////////////////
     $query_id = "SELECT * FROM `membership` WHERE username='$reply_id";
     $result_id = mysql_query($query_id);
     
     $rows = mysql_fetch_array($result_id);
     
     echo $reply_id; 
?>
    <br />
    <table width="80%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td width="10%" bgcolor="#F8F7F1"><p>Reply</td>
    <td width="60%" bgcolor="#F8F7F1"><p><?php echo $reply_message; ?></td>
    </tr>
    <tr>
    <td bgcolor="#F8F7F1"><p>Poster</td>
    <td bgcolor="#F8F7F1"><p><i>Posted By:</i> <font color="red"><?php echo $reply_id; ?></font></p></td>
    </tr>
    </table>
<?php
}  

 

when a user posts on the forum they're username is stored in "reply_id" e.g graham23s

 

so i did this:

 

     $query_id = "SELECT * FROM `membership` WHERE username='$reply_id";
     $result_id = mysql_query($query_id);

 

to match up the username: graham23s with $reply_id but i keep getting

 

Resource id #10 and 11 and so on, i don't seem to be getting the info from the query above, can anyone see any errors i have made?

 

thanks guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/50861-solved-help-retrieving-info-from-mysql/
Share on other sites

The query is correct, however you need to do this to get the data out:

 

<?php
$query_id = "SELECT * FROM `membership` WHERE username='$reply_id'";
$result = mysql_query($query_id);
$row = mysql_fetch_assoc($result); // this will get the first row only.

to get all rows do this:
while ($row = mysql_fetch_assoc($result)) {
     $rows = $row;
}

echo '<pre>', print_r($rows), '</pre>';
?>

Yea, I saw that and fixed it, but your "correction" was incorrect in that anything going into a DB that contains letters should (maybe must) be enclosed in single quotes. Your correction omits the single quotes all together, which would throw an error =)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.