0

i have this array

$row4 = mysql_fetch_array($sql_res);

can anyone help me how to get a random value of $row4['response']?

the $row4 does not have constant values...
but for now $row4 contains

Array ( [0] => 3 [id] => 3 [1] => where is dryad [react] => where is dryad [2] => Dryad is found in the farthest part of the Dark wilderness. [response] => Dryad is found in the farthest part of the Dark wilderness. [3] => [review] => ) 1
4

1 回答 1

3

由于您的代码现在是这样,您只会收到一个查询行。该行将仅包含该行的字段值。如果您确实只是想从该单行获取随机字段值,您将使用:

$randomKey = array_rand($row4,1);

如果您打算从查询中请求随机行,您可以通过以下两种方式之一执行此操作:

1)array_rand用于抓取随机行并放入$randomRow

while($row = mysql_fetch_array($sql_res)) $rows[] = $row;
$randomRow = array_rand($rows);

2)在您的查询中,您可以指定只抓取 1 个随机行而不是每个结果:

SELECT col1 FROM tbl ORDER BY RAND() LIMIT 1;

于 2012-04-04T01:07:42.430 回答