Missing SQL entry with my code

Discussion in 'Web Design and Development' started by Cabbit, Jul 6, 2007.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    Code:
    <?php
    // Database connect //
    include("../include/database.php");
    // do stuff //
    $usrname = $_GET['usrname'];
    if (isset($_GET['id']) and is_numeric($_GET['id'])) {
    $id = $_GET['id'];
    } else {
         exit ('invalid id');
    }
    $sql = mysql_query("SELECT * FROM site_comments WHERE artical_id = $id ORDER BY id DESC");
    if (!$sql) {
         exit ('Error retrieving comments..');
    }
    //Start Loop //
    while ($row = mysql_fetch_array ($sql)) {
    $numofrows = mysql_num_rows($sql); 
    
    print '<table width="100%" border="0" cellpadding="4" cellspacing="0" >';
    
    //start colour changing loop//
    			for($i = 0; $i < $numofrows; $i++)	{
    				$row = @mysql_fetch_array($sql);
    				// the ifelse //
    					if($i % 2) 
    						{ 
    							print '<TR class="pink">';
    						} 
    				else 
    							{ 
    								print '<TR class="wight">';
    							}
    //the output//
    	printf("<td>%s</td><td>%s</td>", $row["3"], $row["2"]);
    //end colour changing loop//
    													}
    															}
    	print "</table>";
    //end //?>
    
    
    <table width="100%" border="0" cellspacing="0" cellpadding="4">
    	<tr>
    	    <td class="addcomment"><a href="javascript:ajaxpage('includes-functions/site_comment_add.php?id=<? print $id ?>&usrname=<? print $usrname ?>', 'comadd');">Add a comment</a></td>
    	</tr>
      		<tr>
    </table>
    <div id="comadd">
    </div>
    Sorry to ask for help again guys, ok the above code works perfict except it always misses the newst entry to the database, if there are 5 it only prints the last 4. i been trying all day to work it out.

    (and yes i dont know much about php and sql im learning a lot as i go though.)
     
  2. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #2
    Solved the code with the following ^_^

    Code:
    if(mysql_num_rows($sql)) // we got some results 
    { 
       $i = 0; 
       while($row = mysql_fetch_array($sql)) 
       { 
          if($i++ % 2) 
          { 
             print '<tr class="pink">'; 
          } 
          else 
          { 
            print '<tr class="wight">'; 
          } 
          //the output// 
          printf("<td>%s</td><td>%s</td></tr>", $row["3"], $row["2"]); 
       } 
       print '</table>'; 
    } 
    else 
    { 
       print '<p class="error">No, Comments</p>'; 
    }
    ?>
     

Share This Page