vendredi 11 novembre 2016

PHP & MYSQL - ORDER BY id sometimes returns nothing

I am trying to wrote a PHP script that will pull a random row from a MYSQL table. So far, the code successfully grabs a random row, but sometimes it will return nothing, and I don't understand why.

$result = $conn->query("SELECT fact FROM numfacts WHERE number = '".(string)$number."' AND id >= (SELECT FLOOR( MAX(id) * RAND()) FROM numfacts ) ORDER BY id LIMIT 1");

The value for $number is currently 12, and there are two rows containing this in the database. Roughly 2/3 times the code returns a value, and the other 1/3 of times it returns 0 results.

If you need more code, I will provide it.




Aucun commentaire:

Enregistrer un commentaire