Jump to content

Warning: Mysql_Fetch_Array(): Need Help


JawdS

Recommended Posts

HI guys,

Everytime I run my php code I get this error message:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a3651864/public_html/footwear.php on line 96

 

This is my code:

 

$query = mysql_query("SELECT * FROM Products WHERE cat='Skate' AND subcat='Footwear' LIMIT $start, $limit_img ");
//end of query
$limit_img = 24;
$start = 0;
$cols = 3;
$cols_td = 0;
//end of table variables
$images = $row['photo'];
$product = $row['product'];
$price = $row['price'];
//end of row variables
echo "<table width='95%'><tr>";
//end of table
while ($row = mysql_fetch_array($query)){
echo "<td><div align='center'>$row[product]</div></td>";
$cols_td = $col_td + 1;
if($cols_td == $cols){
echo "</tr><tr>";
$cols_td = 0;
}
}
//end of while statement
echo "</tr></table>";

Thanks In advance!

Link to comment
https://forums.phpfreaks.com/topic/270824-warning-mysql_fetch_array-need-help/
Share on other sites

That PHP error message most always means that your query is invalid.

 

You should put this at the end of your query statement so we can see the SQL error

$query = mysql_query("SELECT * FROM Products WHERE cat='Skate' AND subcat='Footwear' LIMIT $start, $limit_img ") or die(mysql_error());

As indicated above, it's caused by mysql_query() not returning a resource. It returns FALSE on failure. mysql_fetch_array() expects a resource, but in your case, $query is not a resource. Fixing your query will obviously do the trick, but I would also validate that the query was successful just in case things break in the future.

 


$query = mysql_query("SELECT * FROM Products WHERE cat='Skate' AND subcat='Footwear' LIMIT $start, $limit_img");

if ($query) {
// Query successful

if (mysql_num_rows($query) > 0) {
// Successful and at least one row returned
}

else {
// Successful but no rows returned
}
}

 

In this way, you can present user friendly error messages along with logging for debugging purposes.

Technically, when an associative array element is in a quoted string, the index doesn't need to be quoted. If the index is quoted, complex notation needs to be used, e.g.,

$str = "quoted string with {$array['index']} in it";

I'd look at the order in which these statements occur

 

$query = mysql_query("SELECT * FROM Products WHERE cat='Skate' AND subcat='Footwear' LIMIT $start, $limit_img ");
//end of query
$limit_img = 24;
$start = 0;

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.