将 MySQL 结果存储在单独的 PHP 变量中
问题描述:
我有一个 MySQL 查询,它在运行时返回 3 个随机行.我想将每一行存储在一个数组中,该行的每一列都是数组的不同部分.希望我很清楚.这是我目前所处的位置.
I have a MySQL query that returns 3 random rows whenever it is ran. I want to store each row in an array, with each column of the row a different part of the array. Hopefully Im being clear. Here is where Im at so far.
$result = mysql_query("SELECT NAME,AUTHOR,CITY FROM TESTIMONIALS ORDER BY RAND() LIMIT 3");
$row = mysql_fetch_array($result);
这样做的问题是它将所有内容都存储在 $result 中,一旦存在,我不知道如何引用不同的列.我这样做是为了最终将我想要的指定随机条目打印到 9 个不同的 DIV 中.
The problem with this is it stores EVERYTHING in $result, and I do not know how to reference different columns once it is there. I'm doing this so I can ultimately print the specified random entries that I would like into 9 different DIVs.
答
尝试:
$array = Array();
$result = mysql_query("SELECT NAME,AUTHOR,CITY FROM TESTIMONIALS ORDER BY RAND() LIMIT 3");
while ($row = mysql_fetch_array($result)) {
$array[] = $row;
}