3raser Posted June 25, 2010 Share Posted June 25, 2010 I'm trying to get a users information for each post that is displayed. I know the way my code is set-up is incorrect, but I couldn't think of any other ways. $get_replys = mysql_query("SELECT * FROM posts WHERE tothread='$id'"); while ($row = mysql_fetch_assoc($get_replys)) { $get_user_info = mysql_query("SELECT postcount FROM users WHERE username=$row['username']"); while ($row_info = mysql_fetch_assoc($get_user_info)) { $posts = $row_info['postcount']; $rights = $row_info['rights']; } switch($rights) { default: $rights = "User"; break; case 1: echo $rights = "MOD"; break; case 2: $rights = "ADMIN"; break; } echo "<center><table border='1'><tr><td>Reply posted by <b>". $row['username'] ." (". $posts ." posts - ". $rights .")</b></td></tr></table><table border='1'><tr><td>Posted on ". $row['date'] ."</td></tr><center></center>"; echo "<center><div class='holder'><table border='1'><tr><td><center>". $row['message'] ."</center></td></tr></table></div></center><br/><br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/205813-extracting-a-users-information/ Share on other sites More sharing options...
Pikachu2000 Posted June 25, 2010 Share Posted June 25, 2010 You'll need to clarify what you mean a little bit better. Is there info in the database that you want to display with each post, or . . . ? Quote Link to comment https://forums.phpfreaks.com/topic/205813-extracting-a-users-information/#findComment-1076989 Share on other sites More sharing options...
Andy-H Posted June 25, 2010 Share Posted June 25, 2010 $get_replys = mysql_query("SELECT username, message, date FROM posts WHERE tothread=$id "); while ($row = mysql_fetch_assoc($get_replys)) { $get_user_info = mysql_query("SELECT postcount, rights FROM users WHERE username='{$row['username']}' LIMIT 1")or die(mysql_error()); while ($row_info = mysql_fetch_assoc($get_user_info)) { $posts = $row_info['postcount']; $rights = $row_info['rights']; } switch($rights) { case 1: $rights = "MOD"; break; case 2: $rights = "ADMIN"; break; default: $rights = "User"; break; } $html = <<<HTML <table style="border: 1px solid; margin-left: auto; margin-right: auto; padding: 0;"> <tr> <td>Reply posted by <span style="font-weight: bold;">{$row['username']} ($posts posts - $rights)</span></td> </tr> </table> <table style="border: 1px solid; margin-left: auto; margin-right: auto; padding: 0;"> <tr> <td>Posted on {$row['date']}</td> </tr> <div class="holder" style="margin-left: auto; margin-right: auto;"> <table style="border: 1px solid; margin-left: auto; margin-right: auto; padding: 0;"> <tr> <td style="text-align: center;">{$row['message']}</td> </tr> </table> </div> <br/><br/> HTML; echo $html; } Quote Link to comment https://forums.phpfreaks.com/topic/205813-extracting-a-users-information/#findComment-1076998 Share on other sites More sharing options...
3raser Posted June 25, 2010 Author Share Posted June 25, 2010 $get_replys = mysql_query("SELECT username, message, date FROM posts WHERE tothread=$id "); while ($row = mysql_fetch_assoc($get_replys)) { $get_user_info = mysql_query("SELECT postcount, rights FROM users WHERE username='{$row['username']}' LIMIT 1")or die(mysql_error()); while ($row_info = mysql_fetch_assoc($get_user_info)) { $posts = $row_info['postcount']; $rights = $row_info['rights']; } switch($rights) { case 1: $rights = "MOD"; break; case 2: $rights = "ADMIN"; break; default: $rights = "User"; break; } $html = <<<HTML <table style="border: 1px solid; margin-left: auto; margin-right: auto; padding: 0;"> <tr> <td>Reply posted by <span style="font-weight: bold;">{$row['username']} ($posts posts - $rights)</span></td> </tr> </table> <table style="border: 1px solid; margin-left: auto; margin-right: auto; padding: 0;"> <tr> <td>Posted on {$row['date']}</td> </tr> <div class="holder" style="margin-left: auto; margin-right: auto;"> <table style="border: 1px solid; margin-left: auto; margin-right: auto; padding: 0;"> <tr> <td style="text-align: center;">{$row['message']}</td> </tr> </table> </div> <br/><br/> HTML; echo $html; } Why is it {$row['username']} Why the {}'s Quote Link to comment https://forums.phpfreaks.com/topic/205813-extracting-a-users-information/#findComment-1077437 Share on other sites More sharing options...
Pikachu2000 Posted June 26, 2010 Share Posted June 26, 2010 So you don't have to concatenate like this: username='" . $row['username'] . ''' LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/205813-extracting-a-users-information/#findComment-1077444 Share on other sites More sharing options...
Andy-H Posted June 29, 2010 Share Posted June 29, 2010 Because the arrays are indexed associatively and therefor their keys are stored in the form of strings. Since the mysql query is a string quoted in double quotes, and SQL string values are used (and need to be quotes as such) the braces stop the assoc indexed single quoted strings from messing with the SQL string values. Look at the syntax highlighting. $get_user_info = mysql_query("SELECT postcount, rights FROM users WHERE username='$row['username']' LIMIT 1")or die(mysql_error()); Honestly, for the rest, I can't think why I put them in the braces, probably just to conform with the rest of the code. Quote Link to comment https://forums.phpfreaks.com/topic/205813-extracting-a-users-information/#findComment-1078844 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.