More php help please

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

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    Code:
    <?php
    // Database connect //
    $server = mysql_connect("", "", "");
    $connection = mysql_select_db($server);
    //Connect to stories table //
    $sql = mysql_query('SELECT * FROM `storys` ORDER BY `id` DESC');
    $numofrows = @mysql_num_rows($sql);
    $result = mysql_query("SELECT * FROM storys_comments WHERE story_id = id", $server);
    $num_rows = mysql_num_rows($result);
    //Start Loop //
    print '<table width="100%" border="0" cellpadding="4" cellspacing="0" >';
    	print '<TR class="story_top"><td width="60%">Story</td><td width="15%">Comments</td><td width="15%">Author</td><td width="10%">Poster</td></TR>';
    		while($row = mysql_fetch_array($sql, MYSQL_BOTH)) {
    //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><a href=\"story_display.php?id=%s&usrname=$usrname\">%s</a></td><td>%s</td><td><span style=\"TEXT-ALIGN:center\">%s</span></td><td>%s</td>", $row["id"], $row["title"], $row[$num_rows], $row["author"], $row["poster"]);
    		print "</TR>";
    //end colour changing loop//
    													}
    															}
    	print "</table>";
    //end //
    ?>
    This is the code i gots, what dosnt work is $num_rows prints out the id and not the number of comments the story has, to be truefull i have kinda just glued this into the existing code because they both work quite fine on there own. I will provide the idividual codes below so you can see both working versions.
    Code:
    <?php
    // Database connect //
    $server = mysql_connect("", "", "");
    $connection = mysql_select_db($server);
    //Connect to stories table //
    $sql = mysql_query('SELECT * FROM `storys` ORDER BY `id` DESC');
    $numofrows = @mysql_num_rows($sql);
    $result = mysql_query("SELECT * FROM storys_comments WHERE 
    //Start Loop //
    print '<table width="100%" border="0" cellpadding="4" cellspacing="0" >';
    	print '<TR class="story_top"><td width="70%">Story</td><td width="15%">Author</td><td width="15%">Poster</td></TR>';
    		while($row = mysql_fetch_array($sql, MYSQL_BOTH)) {
    //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><a href=\"story_display.php?id=%s&usrname=$usrname\">%s</a></td><td><span style=\"TEXT-ALIGN:center\">%s</span></td><td>%s</td>", $row["id"], $row["title"], $row["author"], $row["poster"]);
    		print "</TR>";
    //end colour changing loop//
    													}
    															}
    	print "</table>";
    //end //
    ?>
    Code:
    <?php
    $link = mysql_connect("", "", "");
    mysql_select_db("", $link);
    $id = "1";
    $result = mysql_query("SELECT * FROM site_comments WHERE artical_id = $id", $link);
    $num_rows = mysql_num_rows($result);
    echo "(";
    echo "$num_rows";
    echo ")";
    ?>
     
  2. SC68Cal macrumors 68000

    Joined:
    Feb 23, 2006
    #2
    You listed your login credentials on line 3 in the second code box

    First off, you shouldn't do that. That's mistake number one. Create a separate file like "mysql_connect.php" and put all the database connections in that, then simply include (or require) that file inside of your other pages. Oh, and lock it down.
     
  3. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #3
    yips i got a server connect file for my new site i just haven transfered all the updated files yet.
     
  4. SC68Cal macrumors 68000

    Joined:
    Feb 23, 2006
    #4
    Okay, but you've still not removed your username and password from your post. C'mon, take it off there, you're just begging for trouble.
     
  5. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland

Share This Page