Page 1 of 1

Calling a variable in a mysql_query SELECT statement

Posted: Thu Aug 14, 2008 7:19 am
by glennnz
Hi

I have the following:

<?php
$code_link=mysql_connect('localhost', 'username', 'password');
if (!$code_link)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('database', $code_link);
$zv_orders_id = (isset($_SESSION['order_number_created']) && $_SESSION['order_number_created'] >= 1) ? $_SESSION['order_number_created'] : $orders_id;
$code_ordered = mysql_query("SELECT products_id FROM orders_products WHERE orders_products_id = ' " .$zv_orders_id. " ' ");
echo $code_ordered;
if ($code_ordered == "192")
{
$code_quantity = mysql_query("SELECT products_quantity FROM orders_products WHERE orders_products_id = ' " .$zv_orders_id. " ' ");
echo $code_quantity;
}
else
echo "None orderd";
?>

The problems are in the lines in red.

Where am I wrong?

:banghead:

Thanks

G

Re: Calling a variable in a mysql_query SELECT statement

Posted: Thu Aug 14, 2008 7:35 am
by susrisha
what is the variable type for orders_products_id? if its an integer, the single quotes are not necessary in query.

Re: Calling a variable in a mysql_query SELECT statement

Posted: Thu Aug 14, 2008 7:49 am
by glennnz
orders_products_id is an integer, so, instead of

$code_ordered = mysql_query("SELECT products_id FROM orders_products WHERE orders_products_id = ' " .$zv_orders_id. " ' ");

I should have

$code_ordered = mysql_query("SELECT products_id FROM orders_products WHERE orders_products_id = " .$zv_orders_id. " "); ????

I didn't think this would work, the double quotes close in the wrong place??

Re: Calling a variable in a mysql_query SELECT statement

Posted: Thu Aug 14, 2008 8:26 am
by susrisha
your code as a query is written this way

Code: Select all

 
$code_ordered = mysql_query("SELECT products_id FROM orders_products WHERE orders_products_id = " .$zv_orders_id. " ");
 
please try this one instead.. its the same code without using double quotes

Code: Select all

 
$code_ordered = mysql_query("SELECT products_id FROM orders_products WHERE orders_products_id =  $zv_orders_id ");
 
and if there is any error, do let us know

Re: Calling a variable in a mysql_query SELECT statement

Posted: Fri Aug 15, 2008 5:54 am
by glennnz
This one is solved, thanks for everyone's help.

Glenn