Calling a variable in a mysql_query SELECT statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
glennnz
Forum Newbie
Posts: 9
Joined: Thu Aug 14, 2008 7:16 am

Calling a variable in a mysql_query SELECT statement

Post 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
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Calling a variable in a mysql_query SELECT statement

Post by susrisha »

what is the variable type for orders_products_id? if its an integer, the single quotes are not necessary in query.
glennnz
Forum Newbie
Posts: 9
Joined: Thu Aug 14, 2008 7:16 am

Re: Calling a variable in a mysql_query SELECT statement

Post 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??
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Calling a variable in a mysql_query SELECT statement

Post 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
glennnz
Forum Newbie
Posts: 9
Joined: Thu Aug 14, 2008 7:16 am

Re: Calling a variable in a mysql_query SELECT statement

Post by glennnz »

This one is solved, thanks for everyone's help.

Glenn
Post Reply