I am making a mysql query that gathers comments for a user, it works properly, I connect to the database and recieve the comments for a user, but it is not showing the first comment for the user. Here is the code

function comments(){
    //connect to db
    connect_db_members();   
    //start query
    global $number;
    $query = "SELECT * FROM comments WHERE commentie='$number' ORDER BY id DESC";
    //run query
    $results = mysql_query($query);
    //check if there are no comments
    if(mysql_fetch_array($results) == ""){
        echo '<div class="comments_about_wrapper">
  <div class="comment_text_wrapper">
  <div class="comment_text" ><p style="font-size:29px;">You have no comments</p>
  </div>
  </div>
  </div>';

    }
    else {while($row = mysql_fetch_array($results)){
            echo '<div class="comments_about_wrapper">
  <div class="comment_text_wrapper">
  <div class="comment_text"><p>';
  echo nl2br($row['comment']);
 echo ' </p>
  </div>
  </div>
  <div class="comments_about_small_box_wrapper">
  <div class="comments_about_picture">
  <a href=""><img src="" width="45px" height="45px"></a>
  </div>
  </div>
  </div>';       

    }
    }

This is the table comments

id   commenter   commentie   time      comment

5       5         123456      0       EXAMPLE HELLO

6       2         123456      0       EXAMPLE GOODBYE

It will display EXAMPLE GOODBYE but not EXAMPLE HELLO.

Member Avatar for LastMitch

@garyjohnson

I am making a mysql query that gathers comments for a user, it works properly, I connect to the database and recieve the comments for a user, but it is not showing the first comment for the user. Here is the code

I don't see any issue with your code.

But I am curious what is global $number;?

You used $number in your query:

$query = "SELECT * FROM comments WHERE commentie='$number' ORDER BY id DESC";
Member Avatar for diafol

I think it's because you've already run mysql_fetch_array() once before the while loop, so you'll extract record 2 onwards:

if(mysql_fetch_array($results) == ""){
        echo '<div class="comments_about_wrapper">
  <div class="comment_text_wrapper">
  <div class="comment_text" ><p style="font-size:29px;">You have no comments</p>
  </div>
  </div>
  </div>';
    }
    else {while($row = mysql_fetch_array($results)){

Change

if(mysql_fetch_array($results) == ""){

to

if(mysql_num_rows($results) == 0){
commented: Good Catch! +9

@LastMitch

But I am curious what is global $number;?

The global $number is a variable I called in another function, I gather a few pieces of information from the table members and I made the $number global becuase it didnt seem to work when I just placed it in the query. All it is is the commenties id number.

@diafol
I will try what you suggested, thanks for the response!

The suggestion worked, thanks everyone for the responses!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.