If I have a PHP function that generates a random number, is it possible to pass that variable into the sql statement in the WHERE clause? I’m using CodeIgniter, so this is my code using its syntax.
$random = rand(1, 572);
$result = $this->db->query( ' SELECT part1, part2, _id FROM `questions` WHERE `_id` >= '$random' LIMIT 0,1 ');
Is this even possible to do?
EDIT: The reason I want the php to execute the random number is because I need to call it multiple times throughout my pages, and it needs to do another call to another database using a sql query
Yes it is possible if you concatenate the variable with the string:
But if what you want is to select a random row, then you might want this query
EDIT
I understand that
_idwill be random, but you are specifying theminandmaxforrand(), right? So you’d have to change it whenever you insert a new row, or you’d have to use two queries if you want to make surerand()does not return a value too high. By usingORDER BY RAND()you are free from both problems. You simply have to get the value of_idthat was returned from the query.