jon4433 Posted June 4, 2012 Share Posted June 4, 2012 For some reason, my query isn't display the newest joke that is submitted into my database, but is display every joke after the newest submitted one. I don't know why... Could anyone point it out for me, please? <?php session_start(); $_SESSION['username']; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="css/style.css" rel="stylesheet" type="text/css" /> <title>JokeStomp</title> <style type="text/css"> body { background-image: url(images/stripe_aa0c4101186fcf8ef558be7fb7a866d8.png); } </style> </head> <body> <div class="holder"> <?php include("includes/header_template.php"); ?> <div class="menu"> <table width="50%" border="0" cellpadding="4"align="center"> <tr> <td><a href="index.php">Home</a></td> <td>News</td> <td>Contact</td> </tr> </table> </div> <div class="content"> <div class="account"> <?php if (!isset($_SESSION['username']) || empty($_SESSION['username'])) { echo "<p><a href='register.php'>Register</a></p>"; echo "<p><a href='login.php'>Login</a></p>"; }else{ echo "<p><a href='post_joke.php'>Post Joke</a></p>"; echo "<p>Account</p>"; echo "<p><a href='includes/logout.php'>Logout</a></p>"; } ?> </div> <div class="joke"> <?php require ("connect_to_mysql.php"); $sql_query = mysql_query("SELECT * FROM jokes ORDER BY id DESC"); $row = mysql_fetch_array($sql_query) or die(mysql_error()); while($row = mysql_fetch_array($sql_query)){ echo $row['username'] . '<br>' . $row['joke'] . '<br>' . '<br>'; } ?> </div> </div> <?php include("includes/footer_template.php"); ?> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/263653-query-not-displaying-all-data-from-database/ Share on other sites More sharing options...
TOA Posted June 4, 2012 Share Posted June 4, 2012 Get rid of this line and see what happens; it's the only thing that looks out of place to me, although I just scanned it. My guess is that the internal pointer is getting messed up by this first call somehow. $row = mysql_fetch_array($sql_query) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/263653-query-not-displaying-all-data-from-database/#findComment-1351173 Share on other sites More sharing options...
Psycho Posted June 4, 2012 Share Posted June 4, 2012 Quote Get rid of this line and see what happens; it's the only thing that looks out of place to me, although I just scanned it. My guess is that the internal pointer is getting messed up by this first call somehow. $row = mysql_fetch_array($sql_query) or die(mysql_error()); Yes, that is the problem. That line is getting the first record from the database - but it is not used. Then there is a while() loop that starts by getting the second record and displays it. The while loop will then get and output all the subsequent results. In 99% of scenarios you are just going to get the records using the while() loop. Link to comment https://forums.phpfreaks.com/topic/263653-query-not-displaying-all-data-from-database/#findComment-1351175 Share on other sites More sharing options...
jon4433 Posted June 5, 2012 Author Share Posted June 5, 2012 Thank you for the replies, I will do what you said later! Link to comment https://forums.phpfreaks.com/topic/263653-query-not-displaying-all-data-from-database/#findComment-1351355 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.