I have a script that has a GET variable: $_GET['percentage']
I have a MySQL table of data.
Now lets say that there are 100 rows of data in this table.
In pseudo-code:
SELECT data FROM table
Now would it be possible to select $_GET['percentage'] of random data from table?
For example (again in pseudo-code):
$_GET['percentage'] = 10;
SELECT 10% of data from table order by rand()
If this IS possible, how could I do it?
In MySQL, it’s probably easiest to do this in two queries. First, get the count of rows in the table:
Then prepare the query to get random rows:
Then execute the prepared query and send the value of the count divided by 10.
Not every problem needs to be solved by a single query.
Here’s an example PHP script, edited to use the old mysql extension.
PS: Always be careful when interpolating a variable into an SQL expression. You should force the variable to a known format — an integer value in this case. Otherwise you risk creating an SQL Injection vulnerability.