Jump to content

Query not displaying all data from database


jon4433

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.