Jump to content

expects parameter 1 to be resource


MasterACE14

Recommended Posts

I have the following code...

if(!isset($_POST['img']))
    $_POST['img'] = null;
if(!$_POST['img']) die("not a valid product!");

$_POST['img'] =mysql_real_escape_string($_POST['img']);
$img = end(explode('/',$_POST['img']));
$row = mysql_query("SELECT * FROM `ca_shop` WHERE `img`='".$img."'");
$row = mysql_fetch_array($row);

if(mysql_num_rows($row) == 0) die("There is no such product!");

The value of 'img' is

plugins/shopping-cart/img/products/iPod.png

 

and I'm getting the following errors...

<b>Warning</b>:  mysql_query() expects parameter 2 to be resource, null given in <b>shop.php</b> on line <b>7</b><br />

<br />

<b>Warning</b>:  mysql_fetch_array() expects parameter 1 to be resource, null given in <b>shop.php</b> on line <b>8</b><br />

<br />

<b>Warning</b>:  mysql_num_rows() expects parameter 1 to be resource, null given in <b>shop.php</b> on line <b>10</b><br />

There is no such product!

 

I have no idea why?

 

appreciate the help, Thanks

Link to comment
https://forums.phpfreaks.com/topic/207840-expects-parameter-1-to-be-resource/
Share on other sites

You're reusing the variable $row, and you're not checking to make sure that your query was syntactically correct. Try this:

 

<?php
$q = "SELECT * FROM `ca_shop` WHERE `img`='".$img."'";
$rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
if(mysql_num_rows($rs) == 0) die("There is no such product!");
$row = mysql_fetch_array($rs);
?>

 

Ken

after those adjustments Ken I'm now getting...

<b>Warning</b>:  mysql_query() expects parameter 2 to be resource, null given in <b>shop.php</b> on line <b>7</b><br />

Problem with the query: SELECT * FROM `ca_shop` WHERE `img`='iPod.png'<br>

also that query in PHPMyAdmin works.

Are you sure you're connecting to the db successfully?

 

 

EDIT: What ken said, too . . . My first thought when I see those errors is the connection hasn't been made . . .

sorry for wasting your time, completely missed the obvious, this file is called via an AJAX request on another page and I thought the database connection on that page would also be the same as this file called with the AJAX request, thrown in a database connection now and it's working.

 

Thanks again for the help!

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.