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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

I'm not sure what the problem was.  I combined the code from the config file and placed it all in one page, and it worked.  So thank you for your suggestions and ideas.  I am just overlooking some stuff a lot today.

 

Thanks again everyone.

Link to comment
Share on other sites

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]

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.