I need to do a SELECT
PHP where X records are randomly selected. So I'm using the method ORDER BY RAND()
, but I need the to LIMIT
be one or the other depending on a variable, i.e. something like this:
$sql = "SELECT * FROM table ORDER BY RAND() LIMIT = '$limite'";
From what I see that doesn't work, and I don't think I can do it with the "Prepared Statements". If possible, I would like you to apply your answer to the example above, it would be much clearer for me.
Thanks.
The Reference Manual says that a variable can be used with
LIMIT
using prepared queries.Just yesterday I answered about this , only in that case it was code directly in the handler.
If it is by PHP you can do it perfectly through prepared queries:
You have to concatenate your variable to the String:
*Also remove the equality, since the value is directly specified to the LIMIT.
The problem is the
=
one in the declarationyou should use something like: