subzero
subzero

Reputation: 61

how can i use two mysql query with user defined variable in php

select @min_price:=min(prd_sale_price),@max_price:=max(prd_sale_price) from ct_product;
select * from ct_product where prd_sale_price=@min_price or prd_sale_price=@max_price;

This query works in mysql console

but

$query = "
select @min_price:=min(prd_sale_price),@max_price:=max(prd_sale_price) from ct_product;
select * from ct_product where prd_sale_price=@min_price or prd_sale_price=@max_price;
";

$result = mysql_query($query);

this code raise error in php

so, I tried this

$query="
select * from ct_product, (select @min_price:=min(prd_sale_price),@max_price:=max   (prd_sale_price) from ct_product) as b 
where prd_sale_price=@min_price or prd_sale_price=@max_price
";

$result = mysql_query($query);

that works

...

$query = "
select @min_price:=min(prd_sale_price),@max_price:=max(prd_sale_price) from ct_product;
select * from ct_product where prd_sale_price=@min_price or prd_sale_price=@max_price;
";

$result = mysql_query($query);

What's the way that this code would work well without modification as my second way?

Upvotes: 1

Views: 990

Answers (2)

Yehia Awad
Yehia Awad

Reputation: 2978

in PHP mysql_query() can handle only one query at a time

You can't make this method handle 2 query at the same time

what I can suggest is using mysql_query() for every query

Upvotes: 0

Barmar
Barmar

Reputation: 782693

Use two calls to mysql_query:

$query1 = "select @min_price:=min(prd_sale_price),@max_price:=max(prd_sale_price) from ct_product";
$query2 = "select * from ct_product where prd_sale_price=@min_price or prd_sale_price=@max_price";

mysql_query($query1);
mysql_query($query2);

Variables are associated with a database connection, so they'll persist between the calls.

Upvotes: 4

Related Questions