How do I pull row info from MySQL in an if statement

Discussion in 'PHP' started by alhen, Jan 11, 2008.

  1. #1
    This is what I have, but i know it's not right:

    
    if(" . $row['reply_name'] . " !== '') {
    echo "display something";
    } else {
    echo "nothing to display";
    
    PHP:
    I think/hope this will be really simple. Help please?

    Thanks,
    ~alhen
     
    alhen, Jan 11, 2008 IP
  2. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2

    
    $q="select * from table";
    $r=mysql_query($q);
    while ($row = mysql_fetch_array($r)){
    $id=$row['id'];
    
          if($id != 1) {
          echo "I am NOT the #1";
          } else {
          echo "I can only be the #1";
          }
    }
    
    PHP:
     
    LittleJonSupportSite, Jan 11, 2008 IP
  3. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Didn't help... maybe I should post the entire bit of code WITH what you told me to try.

    
    <?  
    MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
    @mysql_select_db( "$database") or die( "Unable to select database"); 
    
    $query="SELECT * FROM $usertable LEFT JOIN blog_reply ON blog.id = blog_reply.reply_id ORDER BY date DESC";
    $result=mysql_query($query) or die(mysql_error());
    
    if (mysql_num_rows($result) == 0) {  echo "<div class=\"text\" align=\"center\"><font color=\"7A7838\"><b>No BLOG entries available</b></font></div>";
    
    } else {
     
    while ($row = mysql_fetch_array($result)) {
    $reply_name = $row['reply_name'];
    echo "<div class=\"text\" align=\"left\"><i>" . $row['day'] . "</b> - " . $row['disp_date'] . "</i><br><b>" . $row['title'] . "</b><br><br>" . $row['comments'] . "<br>
    
    
    <input name=\"id\" type=\"hidden\" value=\"" . $row['id'] . "\">
    <input name=\"reply\" value=\"reply to post\" style=\"background-color:#493C36\" type=\"submit\" class=\"form\"><br>";
    
    if($reply_name != '') {
    echo "<table width=\"100%\"  border=\"0\" cellspacing=\"2\" cellpadding=\"0\" bgcolor=\"#333333\">
      <tr>
        <td colspan=\"2\">Comments: </td>
      </tr>
      <tr>
        <td width=\"3%\">&nbsp;</td>
        <td width=\"97%\" class=\"smalltext\"><b>" . $row['reply_name'] . "</b> - " . $row['disp_date'] . "<br>" . $row['reply_comments'] . "</td>
      </tr>
    </table>
    <br><center><img src=\"images/break.jpg\"></center><br></div>";
    } else {
    echo "No Comments";
    
     }
    }
    ?>
    PHP:
    when tried I get this error on the page:
    Parse error: parse error, unexpected $ in /home/tarag/public_html/blog_page.php on line 83
    which is referring to the "$" in the line:
    if($reply_name != '') {
    PHP:
     
    alhen, Jan 11, 2008 IP
  4. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    always indent your code, easier to read where there is a missing bracket (while statement had one missing)
    
    <?  
    MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
    @mysql_select_db( "$database") or die( "Unable to select database"); 
    
    $query="SELECT * FROM $usertable LEFT JOIN blog_reply ON blog.id = blog_reply.reply_id ORDER BY date DESC";
    $result=mysql_query($query) or die(mysql_error());
    
    if (mysql_num_rows($result) == 0) 
    {
      echo "<div class=\"text\" align=\"center\"><font color=\"7A7838\"><b>No BLOG entries available</b></font></div>";
    }
    else 
    {
    	while ($row = mysql_fetch_array($result)) 
    	{
    		$reply_name = $row['reply_name'];
    		echo "<div class=\"text\" align=\"left\"><i>" . $row['day'] . "</b> - " . $row['disp_date'] . "</i><br><b>" . $row['title'] . "</b><br><br>" . $row['comments'] . "<br>
    		
    		
    		<input name=\"id\" type=\"hidden\" value=\"" . $row['id'] . "\">
    		<input name=\"reply\" value=\"reply to post\" style=\"background-color:#493C36\" type=\"submit\" class=\"form\"><br>";
    	
    		if($reply_name != '') {
    		echo "<table width=\"100%\"  border=\"0\" cellspacing=\"2\" cellpadding=\"0\" bgcolor=\"#333333\">
    		  <tr>
    		    <td colspan=\"2\">Comments: </td>
    		  </tr>
    		  <tr>
    		    <td width=\"3%\">&nbsp;</td>
    		    <td width=\"97%\" class=\"smalltext\"><b>" . $row['reply_name'] . "</b> - " . $row['disp_date'] . "<br>" . $row['reply_comments'] . "</td>
    		  </tr>
    		</table>
    		<br><center><img src=\"images/break.jpg\"></center><br></div>";
    		} else {
    		echo "No Comments";
    		
    		 }
    	}
    }
    ?>
    PHP:
    or you could just add it to the SQL query so it doesnt print out any data for that record

    and reply_name != ''
    Code (markup):
     
    lfhost, Jan 11, 2008 IP
  5. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you so much, that did what I was asking for... but I didn't realize the results.

    I'm trying to create a blog type of function that allows me to post info, then allows comments to each blog. So it'll have multiple comments. As it stands now the comments are showing up, but it creates a new instance of the original post each time.

    Here's a link to what I mean:
    http://www.taragraham.com/blog_page.php

    I'd like it to show the original post one time, then have whatever comments there are underneath it.

    Any help/direction?

    Here's my code now:
    
    <?
    MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
    @mysql_select_db( "$database") or die( "Unable to select database"); 
    
    $query="SELECT * FROM blog LEFT JOIN blog_reply ON blog.id = blog_reply.reply_id ORDER BY date DESC";
    $result=mysql_query($query) or die(mysql_error());
    
    if (mysql_num_rows($result) == 0) 
    {
      echo "<div class=\"text\" align=\"center\"><font color=\"7A7838\"><b>No BLOG entries available</b></font></div>";
    }
    else 
    {
        while ($row = mysql_fetch_array($result)) 
        {
            $reply_name = $row['reply_name'];
            echo "<div class=\"text\" align=\"left\"><i>" . $row['day'] . "</b> - " . $row['disp_date'] . "</i><br><b>" . $row['title'] . "</b><br><br>" . $row['comments'] . "<br>
            
            
            <input name=\"id\" type=\"hidden\" value=\"" . $row['id'] . "\">
            <input name=\"reply\" value=\"reply to post\" style=\"background-color:#493C36\" type=\"submit\" class=\"form\"><br>";
        
            if($reply_name != '') {
            echo "<table width=\"100%\"  border=\"0\" cellspacing=\"2\" cellpadding=\"0\" bgcolor=\"#333333\">
              <tr>
                <td colspan=\"2\">Comments: </td>
              </tr>
              <tr>
                <td width=\"3%\">&nbsp;</td>
                <td width=\"97%\" class=\"smalltext\"><b>" . $row['reply_name'] . "</b> - " . $row['reply_date'] . "<br>" . $row['reply_comments'] . "</td>
              </tr>
            </table>
            <br><center><img src=\"images/break.jpg\"></center><br></div>";
            } else {
            echo "No Comments";
            
             }
        }
    }
    ?>
    
    PHP:
     
    alhen, Jan 13, 2008 IP
  6. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If you are doing the format

    BLOG POST

    BLOG COMMENT
    BLOG COMMENT
    BLOG COMMENT
    BLOG COMMENT
    BLOG COMMENT
    BLOG COMMENT

    You will need to do a single query with a limit of 0,1 to pull the blog post only.
    Then, have a second query which takes the ID from the single query of the blog post, feeds it into query like

    
    $query="SELECT * FROM blog_reply reply_id = '".$blogid.'" ORDER BY date DESC";
    
    //do the while loop here and loop through comments ONLY.
    
    PHP:
     
    lfhost, Jan 13, 2008 IP
  7. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    What I'm looking for is more like this:

    BLOG POST

    BLOG COMMENT
    BLOG COMMENT
    -------------------------
    BLOG POST

    BLOG COMMENT
    -------------------------
    BLOG POST

    BLOG COMMENT
    BLOG COMMENT

    etc...

    would I want to put a limit of 0,1 on the blog post if I'm expecting to post multiple blog entries (with multiple comments)?
     
    alhen, Jan 13, 2008 IP
  8. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    if your pulling more than 1 blog record (why you would show multi comments and multi blog posts on the same page, I am not sure)

    then you would need 2 SQL queries (no limit on the 1st, unless you are paginating the page)

    then inside, have the second while loop, looking up the blogid and loop the comments.
     
    lfhost, Jan 13, 2008 IP