Home > Backend Development > PHP Tutorial > Summary of how to randomly pick up mysql records in php, summary of phpmysql records_PHP tutorial

Summary of how to randomly pick up mysql records in php, summary of phpmysql records_PHP tutorial

WBOY
Release: 2016-07-13 10:10:48
Original
993 people have browsed it

A summary of how php randomly picks mysql records, a summary of phpmysql records

The example in this article summarizes the method of randomly picking mysql records in PHP. Share it with everyone for your reference. The specific analysis is as follows:

To randomly fetch 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. Here is a brief introduction to you.

Method 1, the code is as follows:

Copy code The code is as follows:
select * from tablename order by rand() limit 1

Change the value after limit to the number of items you want to randomly select. Only one is taken here.

Method 2, the code is as follows:

Copy code The code is as follows:
$query= "SELECT count(*) as count FROM recommends";
....
$max_num = $row['count']; // Get the total number of records
srand((double)microtime()*1000000); // Random number seed
$se_pos = rand(0, $max_num); // Random number range
$length = 6; // Number of records
if (($max_num - $se_pos) <= $length) {
$se_pos = $max_num - $se_pos; // When the number of records is less than 6
}

$query = "SELECT * FROM recommendsn limit ".$se_pos.",".$length;

Example 3, assuming there is a database named xyj, there is a table obj in the database, and a field in the table is name. Now we want to randomly select a record from the table. The specific procedure is as follows:
Copy code The code is as follows:
$db = mysql_connect("localhost", "root");
mysql_select_db("xyj",$db);
$result=mysql_query("SELECT * FROM obj",$db);
$max_num=mysql_num_rows($result);//Get the number of records in the database
srand((double)microtime()*10000000); //Generate random number seed.
$se_pos=rand(0, $max_num-1); //Take a random number from 0 to the maximum number of records
$length=30; //Set how many records to fetch in total
//The following is to retrieve the specified number of records.
$result_lim=mysql_query("select * from obj limit $se_pos,$length",$db);
$myrow_lim=mysql_fetch_array($result_lim);
printf("%sn", $se_pos);//Display the randomly obtained record number
printf("%sn", $myrow_lim["name"]);//Display the name field of the randomly obtained record
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/933593.htmlTechArticleSummary of php randomly picking mysql record method, phpmysql record summary This article summarizes the php random pick mysql record method. Share it with everyone for your reference. The specific analysis is as follows: In php, you need to...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template