Jump to content

[SOLVED] OMG! mysql_fetch_array won't work...


TutorMe

Recommended Posts

I went to w3schools, and looked at the MYSQL select page and copied directly from it.  The only thing I changed was the database information and what is echoed.  Here is the code:

<?php 
$result = mysql_query("SELECT * FROM Categories ORDERBY cat_id ASC");
while($row = mysql_fetch_array($result))
  {
  echo $row['cat_id'];
  echo "<br />";
  }
mysql_close($con);
?>

It shows this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Applications/xampp/xamppfiles/htdocs/Source/test.php on line 25

 

I'm assuming it's my test server.  I'm using XAMPP on Mac OS X 10.5.  The thing is, XAMPP come with a mysql example, and it uses the same code, and it works.

 

Any ideas?

 

Link to comment
https://forums.phpfreaks.com/topic/84542-solved-omg-mysql_fetch_array-wont-work/
Share on other sites

Interpim: I beleive mysql would throw the Connection Failed, using password NO error without the db selected not the mysql_fetch_array is invalid

 

<?php 
$result = mysql_query("SELECT * FROM `Categories` ORDER BY `cat_id` ASC") or die(mysql_error());
?>

 

add the or die(mysql_error()) part to your sql query.

I went to w3schools, and looked at the MYSQL select page and copied directly from it.  The only thing I changed was the database information and what is echoed.  Here is the code:

<?php 
$result = mysql_query("SELECT * FROM Categories ORDERBY cat_id ASC");
while($row = mysql_fetch_array($result))
  {
  echo $row['cat_id'];
  echo "<br />";
  }
mysql_close($con);
?>

It shows this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Applications/xampp/xamppfiles/htdocs/Source/test.php on line 25

 

I'm assuming it's my test server.  I'm using XAMPP on Mac OS X 10.5.  The thing is, XAMPP come with a mysql example, and it uses the same code, and it works.

 

Any ideas?

 

Are you connecting to the database in any way? Can you post your entire code?

 

And do the table and column exist in your database?

What do you mean by error reporting?  Could you give me a small example?

 

You should always check the return status of your queries before trying to use any results they may produce. At minimum, as simple SELECT query should look like.

 

<?php

  // connect to db.

  $sql = "SELECT * FROM `Categories` ORDER BY `cat_id` ASC";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        echo $row['cat_id'] . "<br />";
      }
    }
  }

?>

 

Now, depending on what you want to occur when an error is found you could make that script more verbose by using....

 

[code]
<?php

  // connect to db.

  $sql = "SELECT * FROM `Categories` ORDER BY `cat_id` ASC";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        echo $row['cat_id'] . "<br />";
      }
    } else {
      echo "No records found";
    }
  } else {
    echo "An error has occured<br />" . mysql_error() . "<br />$sql";
  }

?>

[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.