Summary of methods for randomly retrieving mysql records from php and phpmysql record _ PHP Tutorial

Source: Internet
Author: User
Tags random seed
Summary of php random mysql record method and phpmysql record. Summary of php random mysql record method, phpmysql record summary examples in this article summarize the php random mysql record method. Share it with you for your reference. The specific analysis is as follows: In php, mysql record methods should be randomly selected with php. Summary of phpmysql record

This example summarizes the php method for random mysql record retrieval. Share it with you for your reference. The specific analysis is as follows:

To randomly retrieve mysql records in php, we can directly use mysql_query to execute and read the data obtained by the select rand function in mysql. here we will give you a brief introduction.

Method 1: The code is as follows:

The code is as follows:

Select * from tablename order by rand () limit 1

Change the value after limit to the number you want to randomly extract. here, only one value is used.

Method 2: The code is as follows:

The code is as follows:

$ Query = "SELECT count (*) as count FROM recommends ";
....
$ Max_num = $ row ['count']; // Retrieve the total number of records
Srand (double) microtime () * 1000000); // random 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 there are less than 6 Records
}

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


Example 3: assume that there is a database named xyj with the table obj in the database and the field name in the table. now we need to randomly select a record from the table. the specific procedure is as follows:

The code is as follows:

<? Php
$ Db = mysql_connect ("localhost", "root ");
Mysql_select_db ("xyj", $ db );
$ Result = mysql_query ("SELECT * FROM obj", $ db );
$ Max_num = mysql_num_rows ($ result); // Obtain the number of database records
Srand (double) microtime () * 10000000); // generates a random seed.
$ Se_pos = rand (0, $ max_num-1); // random number from 0 to the maximum number of records
$ Length = 30; // set the total number of records
// The following figure shows 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 random record number
Printf ("% sn", $ myrow_lim ["name"]); // display the name field of the random record
?>

I hope this article will help you with php programming.

Examples in this article summarize the methods for randomly retrieving mysql records from php. Share it with you for your reference. The specific analysis is as follows...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.