PHP random fetch MySQL Record method summary, Phpmysql record summary
This paper summarizes the PHP random fetch MySQL record method. Share to 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's a brief introduction to you.
Method One, the code is as follows:
Copy the code as follows: SELECT * FROM TableName ORDER by rand () limit 1
Change the value behind the limit to the number of bars you want to randomly extract, and take only one.
Method Two, the code is as follows:
Copy CodeThe code is as follows: $query = "SELECT count (*) as Count from recommends";
....
$max _num = $row [' count ']; Total Records taken
Srand (Double) microtime () *1000000); Random number Seed
$se _pos = rand (0, $max _num); Random number Range
$length = 6; Number of record strips
if (($max _num-$se _pos) <= $length) {
$se _pos = $max _num-$se _pos; The number of records is less than 6.
}
$query = "SELECT * from Recommendsn limit". $se _pos. ",". $length;
Example 3, suppose there is a database named Xyj, the library has the table obj, the table has a field name, now to implement a random selection from the table a record, the program is as follows:
Copy CodeThe 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);//Gets the number of records in the database
Srand (Double) microtime () *10000000); Generates a random number seed.
$se _pos=rand (0, $max _num-1); Random number from 0 to maximum record count
$length = 30; Set the number of records to be taken together
The following is the removal of 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 randomly obtained record number
printf ("%sn", $myrow _lim["name"]);//Display the Name field of a randomly obtained record
?>
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/933593.html www.bkjia.com true http://www.bkjia.com/PHPjc/933593.html techarticle PHP random fetch MySQL Record method summary, Phpmysql record Summary This article summarizes the PHP random fetch MySQL record method. Share to everyone for your reference. The specific analysis is as follows: in PHP to follow ...