Jump to content

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!

Edited by Zane
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";

Edited by Pikachu2000

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;

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.