Home > Article > Backend Development > Example of how to randomly pick mysql records in php_PHP tutorial
To randomly select mysql records in php, we can directly use mysql_query to execute the data obtained by the select rand function in mysql and read it out. Let me introduce you to a specific example.
Method 1:
The code is as follows
|
Copy code
|
||||||||||||||||
select * from tablename order by rand() limit 1
Change the value after limit to the number of records you want to randomly select. Only one is taken here.
$se_pos = rand(0, $max_num); //Random number range Suppose there is a database named xyj. There is a table obj in the database. There is a field in the table called name. Now we want to randomly select a record from the table. The specific procedure is as follows:
|