Jump to content
Larry Ullman's Book Forums

Incorrect Query For Viewing Postings


Recommended Posts

Help! Its me again.
I have a page for displaying a forum members' postings.
It displays the postings nicely but it has two problems:

1. The posting dates are in random order instead of ascending order.

2. It displays the following error message:

Notice: Use of undefined constant uname - assumed 'uname' in C:\xampp\htdocs\theforum\view_posts.php on line 24

I am obviously doing something silly in Line 24, can you locate the mistake please?
 

<?php
// Start the session.
session_start() ;
// Redirect if not logged in.
if ( !isset( $_SESSION[ 'member_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }
include ( 'includes/header_wise.php' ) ;
// Connect to the database
require ( 'mysqli_connect.php' ) ;
// Make the query. THE NEXT LINE IS LINE NUMBER 24
$q = "SELECT uname,post_date,subject,message FROM forum WHERE uname = '{$_SESSION[uname]}' ORDER BY 'post_date' ASC";
$result = mysqli_query( $dbcon, $q ) ;
if ( mysqli_num_rows( $result ) > 0 )
{
  echo '<h2>Your Postings</h2><table><tr><th>Posted By</th><th>Forum</th><th id="msg">Quotation</th></tr>';
  while ( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC ))
  {
    echo '<tr><td>' . $row['uname'].'<br>'.$row['post_date'].'</td>
    <td>'.$row['subject'].'</td><td>' . $row['message'] . '</td> </tr>';
  }
  echo '</table>' ;
}
//The rest of the code goes here
?>
Link to comment
Share on other sites

konfused, in the future, please let us know what line 24 is, or it's hard for us to help.

 

Your error in line 24 is that you're using $_SESSION[uname], but you should be using $_SESSION['uname'].

Also, assuming that the post_date column is of the type DATETIME or TIMESTAMP, using an ORDER BY on that column should put them in order. Is that not the case?

  • Upvote 2
Link to comment
Share on other sites

You don't need the quotes around post_date in your order by clause

ORDER BY post_date ASC"

I've had problems before with ordering by a timestamp field where the order is by the month, so you may need to do some date manipulation. Let me know if its not working and I'll see if I can dig up an example.

  • Upvote 2
Link to comment
Share on other sites

Excellent catch, Margaux! I expect that's the issue. The query results were literally being ordered by the string "post_date", which is to say all of the results were returned in the same primary order (the string), but could be in any random order within that. Remove those quotes and the problem will go away.

Link to comment
Share on other sites

Many thanks Hartley San and Margaux

 

Correcting the quotes did the trick. When to use quotes is the key, I hope to master the rules soon.

 

Hartley San, I did point out which line was Number 24. My comment preceding line 24 was as follows:

 

// Make the query. THE NEXT LINE IS LINE NUMBER 24

 

Thanks again for your patience.

Link to comment
Share on other sites

  • 2 weeks later...
 Share

×
×
  • Create New...