mysql_fetch_array() Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Adrian
Forum Newbie
Posts: 2
Joined: Mon Feb 27, 2006 10:11 am

mysql_fetch_array() Error

Post by Adrian »

Ive been coding with php and mysql for a while but im doing a database driven website atm and keep getting this same error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/adriantr/public_html/joke.php on line 6
Ive checked my code and everything looks fine. Here is the code below. Please tell me why i'm getting this error! :?

Code: Select all

<?php
include("admin/config.php");
$jokeid = $_GET["jokeid"];
$sql = "";
      $result = mysql_query("SELECT * FROM news WHERE jokeid=$jokeid");        
      $row = mysql_fetch_array($result);
	  $joketitle = $row["joketitle"];
	  $thejoke = $row["thejoke"];
	  $jokeimgurl = $row["jokeimgurl"];
	  $jokeviews = $row["jokeviews"];
	  $jokeviews = $jokeviews + 1;
	  echo("<table width=\"500\" border=\"0\" cellspacing=\"00\" cellpadding=\"5\">
  <tr>
    <td width=\"50\"><img src=$jokeimgurl width=40 height=40></td>
    <td>$joketitle - Date Added: $jokedate - Number of Views: $jokeviews </td>
  </tr>
  <tr>
    <td></td>
    <td>$thejoke</td>
  </tr>
</table>");
mysql_close();
$sql = "UPDATE jokes SET jokeviews='$jokeviews' WHERE jokeid=$jokeid";
      $result = mysql_query($sql);
?>
Thanks in advance!
-Adrian
Last edited by Adrian on Mon Feb 27, 2006 10:48 am, edited 2 times in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

It's a code problem, so it goes in the code forum.. you were right the first time :P

Also, please use PHP tags when posting PHP code.

Anyway..

That error is because, funnily enough, the supplied argument is not a valid MySQL resource.

99/100 it is because one of the previous steps with the mysql_* functions failed somewhere and so the suppled argument is a boolean value of false.

on the mysql_connect() and mysql_query() functions, extend them with 'or die(mysql_error());' like so:

Code: Select all

mysql_query('SELECT * FROM `table`') or die(mysql_error());
That will give a more definitive error message.

But once you go into production (i.e. it works) remove the or die()'s as they can give away sensitive information.
Adrian
Forum Newbie
Posts: 2
Joined: Mon Feb 27, 2006 10:11 am

Post by Adrian »

Fixed the code/php thing in my last post. Thank you. You have sorted my problem. Got it working in the end. Thanks for the help!
Post Reply