1

我觉得自己像门把手一样笨。我能得到一些关于为什么我没有在这里返回结果的建议吗?请善待我,让我摆脱痛苦。

$query = mysql_query
(
   "SELECT first_name, 
           COUNT(first_name) AS answer
    FROM orders 
    GROUP BY first_name 
    ORDER BY answer DESC
    LIMIT 1"
); 

echo "<p>The most frequent customer is $result</p>";
4

3 回答 3

1

检查这个..

$query = mysql_query("SELECT first_name, COUNT(first_name) AS answer 
FROM orders 
GROUP BY first_name 
ORDER BY answer DESC
LIMIT 1"); 

$result = mysql_fetch_assoc($query);
echo "<p>The most frequent customer is " . $result["first_name"] . "</p>";
于 2013-09-12T11:39:59.523 回答
0

尝试这个:

$result = mysql_query("SELECT first_name, COUNT(first_name) AS answer FROM orders GROUP BY first_name ORDER BY answer DESC LIMIT 1");
$array = mysql_fetch_assoc($result);
echo "<p>The most frequent customer is ".$array[0]['first_name']."</p>";
于 2013-09-12T11:41:30.040 回答
0

mysql_fetch_array()执行查询后,您错过了对的调用。查询也可以是INSERT操作或类似的。它只返回有关请求本身的信息(例如,它是否成功,是否附加了结果?),而不是信息本身。

这与数据库本身有关:信息作为所谓的游标返回,您可以根据需要对其进行迭代。因此,并非所有结果都必须一次保存在内存中。例如,在您的情况下,您不能只有一个结果,而是一百万行。在某些时候,这对你的记忆来说可能太多了。然后,游标和逐行迭代有很大帮助。

因此,您需要致电:

$query = mysql_query("SELECT first_name, COUNT(first_name) AS answer 
FROM orders 
GROUP BY first_name 
ORDER BY answer DESC
LIMIT 1");

$result = mysql_fetch_array($query); // will retrieve the first row from the result
// if you have multiple rows (not LIMIT 1), you would have to call it again and again

echo "<p>The most frequent customer is $result['first_name']</p>";
于 2013-09-12T11:42:09.810 回答