I have a SQL table with only two columns: “nameid” and “name”. I want to get a random name out of the database.
Before I did this via:
$result = mysql_query("SELECT * FROM nametable",$db);
$number = mysql_num_rows($result);
$random = rand(1,$number);
list($name) = mysql_fetch_row(mysql_query("SELECT name FROM nametable WHERE nameid=$random",$db));
How can I do the same with Doctrine?
MySQL has a function for generating random floating point values
RAND(). When sorting by this, the names will be randomly-ordered. After this, you simply select the first, randomly-ordered name.With Doctrine, this could be done using