2

How do you get the row returned by this SQL query using PHP:

SET @r := (SELECT ROUND(RAND() * (SELECT COUNT(*) FROM mytable)));
SET @sql := CONCAT('SELECT * FROM mytable LIMIT ', @r, ', 1');
PREPARE stmt1 FROM @sql;
EXECUTE stmt1;

Setting $query equal to the above and then doing the usual mysql_query($query) is not returning any results.

Community
  • 1
  • 1
ProgrammerGirl
  • 3,059
  • 6
  • 41
  • 78

1 Answers1

0

Do it like this to see if it works

mysql_query('SET @r := (SELECT ROUND(RAND() * (SELECT COUNT(*) FROM mytable)));');
mysql_query('SET @sql := CONCAT('SELECT * FROM mytable LIMIT ', @r, ', 1');');
$resource = mysql_query('PREPARE stmt1 FROM @sql;EXECUTE stmt1;');

Dont forget to escape the sql in the second instruction

Muhammad Raheel
  • 19,645
  • 7
  • 66
  • 101