eleven0 Posted February 17, 2008 Share Posted February 17, 2008 I'm currently changing my site from HTML to PHP. I'm making a simple CMS. include 'config.php'; include 'opendb.php'; // Make a MySQL Connection $query = "SELECT * FROM cms"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $title= $row['first']; $cont= $row['last']; echo "<br />"; } include 'closedb.php'; echo "<td valign='top'><div class='tborder22'><b> $title </b> </div>"; echo $cont; } I got a title and an article. Above outputs the article under its title. But it only outputs the last entry. How can I show all entries in that form? Link to comment https://forums.phpfreaks.com/topic/91500-output-data-from-database/ Share on other sites More sharing options...
uniflare Posted February 17, 2008 Share Posted February 17, 2008 the reason your script only echoes the last entry from what i can tell is your logic with the while loop. Try Echoing during the while loop, otherwise you will need to store $title and $cont as arrays, so that each time the while() loops it wont overwrite whatever is stored already (which is why only the last entry survives). Try This: include 'config.php'; include 'opendb.php'; // Make a MySQL Connection $query = "SELECT * FROM cms"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); // Store entire array $x = count($row); // Count Rows For($i=0;$i<$x;$i++){ $title[$i] = $row[$i]['first']; // $row[row_number][Field_Name_or_Number] $cont[$i] = $row[$i]['last']; } include 'closedb.php'; // Do not need in this example, but for larger scripts you may want to recalculate $x to be safe For($i=0;$i<$x;$i++){ echo "<td valign='top'><div class='tborder22'><b> $title[$i] </b> </div>"; echo $cont[$i]; echo "<br />"; } I replaced the While loop with a For loop for ease of assigning arrays. you can do it with either really but this is typically how i would code this personally. I noticed in the while loop you were only echoing a <br> ? the only reason i can see is to count the <br>'s for debugging purposes. Also i noticed an extra "}" end bracket, from the code visible in your script you dont need it. Any question please ask, hope this helps Link to comment https://forums.phpfreaks.com/topic/91500-output-data-from-database/#findComment-468704 Share on other sites More sharing options...
eleven0 Posted February 17, 2008 Author Share Posted February 17, 2008 It didn't give me any errors but it didn't output anything. Link to comment https://forums.phpfreaks.com/topic/91500-output-data-from-database/#findComment-468735 Share on other sites More sharing options...
uniflare Posted February 17, 2008 Share Posted February 17, 2008 hmm no output would suggest an empty result (your query matched 0 rows, or the table is empty) try this and tell me what it says: <?php include 'config.php'; include 'opendb.php'; // Make a MySQL Connection $query = "SELECT * FROM cms"; $result = mysql_query($query) or die("Mysql ERROR: ".mysql_error()); $row = mysql_fetch_array($result); // Store entire array $x = count($row); // Count Rows echo("<br>Row Count: $x<br>"); // debug echo For($i=0;$i<$x;$i++){ $title[$i] = $row[$i]['first']; // $row[row_number][Field_Name_or_Number] $cont[$i] = $row[$i]['last']; } print_r($title);print_r($cont); // Debug Echo include 'closedb.php'; // Do not need in this example, but for larger scripts you may want to recalculate $x to be safe For($i=0;$i<$x;$i++){ echo "<div class='tborder22'><b> $title[$i] </b> </div>"; echo $cont[$i]; echo "<br />"; } ?> i have added debug lines to try and solve this mystery Link to comment https://forums.phpfreaks.com/topic/91500-output-data-from-database/#findComment-468774 Share on other sites More sharing options...
eleven0 Posted February 17, 2008 Author Share Posted February 17, 2008 Row Count: 6 Array ( [0] => 2 [1] => s [2] => a [3] => [4] => [5] => ) Array ( [0] => 2 [1] => s [2] => a [3] => [4] => [5] => ) 2 (id number of my first entry, I removed the first one so it's 2.) 2 s (First letter of my first entry's title) s a (First letter of my first entry's context) a Then I get 3 blank title lines. Here is the page source of above; <br>Row Count: 6<br>Array ( [0] => 2 [1] => s [2] => a [3] => [4] => [5] => ) Array ( [0] => 2 [1] => s [2] => a [3] => [4] => [5] => ) <div class='tborder22'><b> 2 </b> </div>2<br /><div class='tborder22'><b> s </b> </div>s<br /><div class='tborder22'><b> a </b> </div>a<br /><div class='tborder22'><b> </b> </div><br /><div class='tborder22'><b> </b> </div><br /><div class='tborder22'><b> </b> </div><br /></td> </tr> Link to comment https://forums.phpfreaks.com/topic/91500-output-data-from-database/#findComment-468951 Share on other sites More sharing options...
eleven0 Posted February 17, 2008 Author Share Posted February 17, 2008 anyone? Link to comment https://forums.phpfreaks.com/topic/91500-output-data-from-database/#findComment-468978 Share on other sites More sharing options...
revraz Posted February 17, 2008 Share Posted February 17, 2008 May have to mess with your tables, but you should get the idea. include 'config.php'; include 'opendb.php'; // Make a MySQL Connection $query = "SELECT * FROM cms"; $result = mysql_query($query) or die(mysql_error()); echo "<td valign='top'><div class='tborder22'><b> $title </b> </div>"; while($row = mysql_fetch_array($result)){ echo "<tr><td>{$row['first']}</td>"; echo "<td>{$row['last']}</td>"; echo "</tr>>"; } include 'closedb.php'; } Link to comment https://forums.phpfreaks.com/topic/91500-output-data-from-database/#findComment-469026 Share on other sites More sharing options...
eleven0 Posted February 18, 2008 Author Share Posted February 18, 2008 what if I want to show a specific ID article? Like lets say 2nd article in databse. What would i have to do? Link to comment https://forums.phpfreaks.com/topic/91500-output-data-from-database/#findComment-469255 Share on other sites More sharing options...
uniflare Posted February 18, 2008 Share Posted February 18, 2008 sry for not replying, went to bed . did revraz solution fix your problem? (i believe i accounted for a different result set in my code). ----- To answer your last question all you have to do is change your query: $query = "SELECT * FROM cms WHERE `fieldname`='value'"; ie: $query = "SELECT * FROM cms WHERE `id`='2'"; Link to comment https://forums.phpfreaks.com/topic/91500-output-data-from-database/#findComment-469348 Share on other sites More sharing options...
eleven0 Posted February 18, 2008 Author Share Posted February 18, 2008 Yea, That fixed that problem. But I couldn't fit that format to my site. Tables are messed up. I'm going to try to output them separately. Thanks to both of you. Link to comment https://forums.phpfreaks.com/topic/91500-output-data-from-database/#findComment-469720 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.