0

i am trying to do a guestbook in php but i am having some problems with mysql_fetch_array function. I don't understand why. I try to debug by putting die("Error ".mysql_error()) but nothing prints out. I guarantee that all my variables are correctly initialized. Here is my code :

<?php

 $nbmessagesPP = 10;
 mysql_connect(HOST, USER,PASSWORD) or die( "Unable to connect to database");
 mysql_select_db(DBNAME) or die ("Unable to select database!"); 

 .......

 if(isset($_GET['page'])){
    $page = $_GET['page'];
  } else {
    $page = 1;
  }
  $first_msg = ($page - 1) * $nb_of_Page;
  $query = 'Select * from livredor ORDER BY id DESC LIMIT '.$first_msg.', '.$nbmessagesPP;
  $rep = mysql_query($query) or exit("Error in query".mysql_error());

  $v = true;
  while($v){
      $v = ($data = mysql_fetch_array($rep) or die ("Error fetching the data : ".mysql_error()));
      echo "<p>id -> ".$data['id']."</p>";    
      echo "<p>pseudo ->".$data['pseudo']."</p>";
      echo "<p>messages ->".$data['message']."</p>";
      echo "<hr/>";
  } 
  mysql_close();
?> 

Can someone help me ;)

4
  • 6
    i am having some problems with mysql_fetch_array | which problems? Not sure but try to do it like everyone does: while($data = mysql_fetch_array($rep)) {} Commented Jul 7, 2010 at 12:50
  • maybe not have data in the table ? Commented Jul 7, 2010 at 12:56
  • "while($data = mysql_fetch_array($rep)) {}". When i do this, i cannot run my script, in firefox it said that the connection was reset by ther server. I know it is a bad way, but this is the only way i am using to display my values. Commented Jul 7, 2010 at 13:06
  • I remember having this error : "mysql_fetch_array(): supplied argument is not a valid MySQL result resource" Commented Jul 7, 2010 at 13:11

4 Answers 4

3

Your code doesn't deal with errors or the last row correctly. When $v is false, it still goes on to print some data. It would be better rewritten as:

while (($data = mysql_fetch_array($rep))) {    
  echo   
  ... 
}

That forces the evaluation of the fetch before moving on to the printing.

Sign up to request clarification or add additional context in comments.

5 Comments

yeah i know, but when i put it in the while loop, i cannot even run my script. In firefox, it says that the connection was reset by the server.
You have an extra parenthesis there.
@You: intended... I always put extra parenthesis when using an assignment as part of a comparison. In this case it doesn't matter much, but if it was ( (x=y) == 5) it makes a big difference.
In that case you're missing one. There's three ( and only two ) ;)
@You: lol... I went to fix it last time, said, "wait, no I meant that", and didn't even notice it was still wrong. Thanks.
0

The problem is that you're trying to access elements of the result that don't exist. mysql_fetch_array returns a regular array, with integer indices. What you want is mysql_fetch_assoc, which returns an associative array.

Edit: You also have the problem Chris describes, not dealing with the last row correctly.

2 Comments

I still have the same probl by using mysql_fetch_assoc.
I remember having this error : "mysql_fetch_array(): supplied argument is not a valid MySQL result resource"
0

Generally, if you're receiving an error saying "supplied argument is not a valid MySQL result resource" it means that your MySQL query has failed, therefor not returning a valid result resource.

Try to echo out $query before sending it through mysql_query(), then try placing the echo'd query into phpMyAdmin and see if it returns any results.

Comments

0

Ok i have found the problem. The problem was that in another page i had a mysql_connection and in that page i was creating a new one. I just catch the return value of mysql_connect function and then close it with mysql_close function at the end. Like this :

  <?php
     $link = mysql_connect(HOST, USER,PASSWORD) or die( "Unable to connect to database");
     mysql_select_db(DBNAME) or die ("Unable to select database!"); 
     .....

     while($data = mysql_fetch_array($rep)) {//i do something here}

     mysql_close($link);    
 ?>

Thanks for your answers folks :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.