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
Brad Fox
  • 685
  • 6
  • 19
Julian Paolo Dayag
  • 3,562
  • 3
  • 20
  • 32

1 Answers1

3

As your code is right now, you would only receive back one query row. This row would just contain your field values for that row. If you are indeed just trying to get a random field value from that single row, you would use:

$randomKey = array_rand($row4,1);

If you meant to ask for a random row from your query, you can do this one of two ways:

1) Use array_rand to grab a random row and place into $randomRow:

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

2) In your query you can specify to only grab 1 random row rather than every result:

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

VictorKilo
  • 1,839
  • 3
  • 24
  • 39
  • sir can you tell me how.... to get the response value of the random row???? i only want the random response – Julian Paolo Dayag Apr 04 '12 at 01:13
  • If you are using the $randomKey method tat means that you are not receiving an array, only a single random value from your one row returned. If you want to reference a random row (which would be an array) use one of the other methods. – VictorKilo Apr 04 '12 at 03:28