gpt4 book ai didi

php - 将 mysql 结果获取到 php 变量时遇到问题

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

我有以下 PHP 代码:

$count = "select 
count(fruit) as total,
sum(fruit like '%apple%') as apple,
sum(fruit like '%orange%') as orange
FROM my_wonderful_table_of_fruits";

$count = mysql_query($count);
$count = mysql_fetch_row($count);

我试图将它们存储在变量中,但似乎无法捕获它们:/

我的代码是:

while ($row = mysql_fetch_array($count)) {
$count_total = $row['total'];
$count_apple = $row['apple'];
$count_orange = $row['orange'];
}

我希望能够像这样回应他们:

echo "$count_total[0] 是 $count_apple 个苹果和 $count_orange 橙子的总数

当我在 MySQL Admin 中运行此查询时,我得到了一个漂亮的行,如下所示:

total    apple    orange
5 3 2

有人知道我做错了什么吗? (除了我使用 mysql_fetch_row 的“邪恶”版本这一事实)

非常感谢!

最佳答案

由于您的查询仅生成一行,因此您可以将其简化为以下内容:

list($total,$apple,$orange) = mysql_fetch_row(mysql_query("
SELECT COUNT(`fruit`) AS `total`,
SUM(`fruit` LIKE '%apple%') AS `apple`,
SUM(`fruit` LIKE '%orange%') AS `orange`,
FROM `my_wonderful_table_of_fruits`"));
echo "$total is the total of $apple apples and $orange oranges.";

关于php - 将 mysql 结果获取到 php 变量时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12255446/

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