gpt4 book ai didi

php - 尝试检索最小值时出现 mysql "select"问题

转载 作者:行者123 更新时间:2023-11-29 18:15:38 25 4
gpt4 key购买 nike

我的数据库中有几行以下数据:

 name    price
iphone 1000
iphone 888
iphone 1010

我试图为 iPhone 获取最便宜的价格,但我的脚本返回最昂贵的价格

class getdbdata {

function cheapestprice($nameofproduct) {

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mobilephones";
$pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username,
$password);
$sql = ("SELECT name,price FROM phones where name like
'%".$nameofproduct."%' and price =(SELECT MIN(price) FROM phones)");
$stmt = $pdo->prepare($sql);
$stmt->execute() ;
$posts= $stmt->fetch() ;
foreach ($posts as $post ) {
echo $post;
}
var_dump($posts);
}

}

$getdbdata = new getdbdata();
$getdbdata->cheapestprice("iphone");

最佳答案

将查询更改为:

SELECT name, Min(price) FROM phones where name like '%".$nameofproduct."%'

关于php - 尝试检索最小值时出现 mysql "select"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47052617/

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