getting a variable into mysql_query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
adrigen
Forum Newbie
Posts: 9
Joined: Tue Oct 07, 2003 3:32 am
Contact:

getting a variable into mysql_query

Post by adrigen »

Hi,
Im just wondering how to get a variable into this following line. the variable is $CID.
As it is i get "supplied argument is not a valid MySQL-Link resource"


mysql_select_db("ce",$db);
$query2 = mysql_query("SELECT * FROM tblProducts WHERE CategoryID = %s", $CID);
if ($myrow = mysql_fetch_array($query2)) {

Any ideas? sorry im a beginner.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It's giving you the error because you are passing $CID as an argument to the mysql_query() function which only takes two - 1: the query itself and 2: the link resource. $CID isn't a link resource ($db is however) so it's not working.

If you change the code to:

Code: Select all

mysql_select_db('ce', $db);

$sql = "SELECT * FROM tblProducts WHERE CategoryID = $CID";
$query2 = mysql_query($sql, $db) or die(mysql_error().'<p>'.$sql.'</p>');

if ($myrow = mysql_fetch_array($query2)) {
it should remove that error.

Mac
adrigen
Forum Newbie
Posts: 9
Joined: Tue Oct 07, 2003 3:32 am
Contact:

Post by adrigen »

Thanks, I understand. (and got it to work :lol: )
Adrian
Post Reply