Jump to content

Recommended Posts

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/>";
     }	

Link to comment
https://forums.phpfreaks.com/topic/205813-extracting-a-users-information/
Share on other sites

   $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;
      }

   $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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.