gpt4 book ai didi

php - 我如何在php中使用两个带有用户定义变量的mysql查询

转载 作者:行者123 更新时间:2023-11-29 04:45:03 26 4
gpt4 key购买 nike

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;

此查询在 mysql 控制台中有效

但是

$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);

此代码在 php 中引发错误

所以,我试过了

$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);

有效

...

$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);

作为我的第二种方式,该代码在不修改的情况下如何运行良好?

最佳答案

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);

变量与数据库连接相关联,因此它们会在调用之间持续存在。

关于php - 我如何在php中使用两个带有用户定义变量的mysql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20484919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com