Home >Backend Development >PHP Tutorial >php+mysql realizes random database rearrangement example, phpmysql rearrangement example_PHP tutorial

php+mysql realizes random database rearrangement example, phpmysql rearrangement example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:16:58684browse

php+mysql implements random database rearrangement example, phpmysql rearrangement example

The example in this article implements the method of random rearrangement of the php+mysql database. All the data in the table can be randomly read out once and then randomly saved to another table, thus achieving the function of random recording.

The main implementation code is as follows:

Copy code The code is as follows:
//The database connection is not written here
$s = isset( $_GET['s'] )?$_GET['s']:0;
$e = isset( $_GET['e'])?$_GET['e']:50;
$count =85000;
if( $s < $count )
{
$sql = "select * from table prefix_info where isget =0 order by id desc limit $s,$e ";
$query = mysql_query( $sql );
while( $rs = mysql_fetch_array( $query ) )
{
$id = $rs['id'];
$sss = $rs['sss'];
$typeid = $rs['typeid'];
$isget = $rs['isget'];
$sql = "insert into table prefix_info_bak (id, table prefix, typeid, isget) values ​​('$id','$sss','$typeid','$isget')";
mysql_query( $sql ) ;
echo $sql;
//exit;
$sqlu = "update table prefix_info set isget=1 where id =".$rs['id'];
mysql_query( $sqlu );
}
echo 'Data is being processed, currently '.$ s.'Article. . . . . . ';
}
else
{
echo 'Complete all data processing Randomly sort again';
}
?>

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

php reads the mysql database and randomly displays the specified number of records

1) Just use sql statement to randomly take out 5 records;
mysql is as follows: "select * from youtable order by rand() limit 5";

2) Also randomly pick 5-10 Bar:
$num=rand(5,10);
$sql="select * from youtable order by rand() limit $num";

ps: Why is it not a PHP classification problem?

How to randomly extract several pieces of data (for example, five pieces of data) from the data table student in php+mysql?

It is not a good habit to directly use select * from student order by rand() limit 5. This must take into account the size of the database. For example, if the data volume is more than 10W, the query efficiency is very low and the resources It also consumes a lot! You can consider combining php with mysql to implement it. For example: first use mysql's count() to calculate the total number of students $sum_num, assign it to a variable in php, and then generate this total number in php. Random numbers (this involves whether your 5 pieces of data are 5 consecutive random numbers or 5 scrambled random numbers). If you generate continuous data here, you only need to generate a random number less than $sum_num-5. ! Based on this random number, you can limit out 5 items; if you need to generate scrambled random numbers here, you need to generate 5 different random numbers that are smaller than $sum_num, and finally query in the database based on these random numbers. The first method In this case, limit random number, 5; is used; in the second case, in (random number 1, random number 2, random number 3, random number 4, random number 5) is used; in the case of large amount of data, such efficiency In terms of resource consumption, it is much better than select * from student order by rand() limit 5. Of course, if the amount of data is not large, you can use this! Just to spread the knowledge to you! I wish you a happy study! (For the time being, I assume that you can use PHP to generate random numbers and the random numbers generated by PHP here must be an integer greater than 0. If not, you can use Baidu or Google first, and finally you can continue to leave me a message)!
Because your ID may be missing, some methods are not applicable! I won’t list them one by one!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/895114.htmlTechArticlephp+mysql realizes random database rearrangement example, phpmysql rearrangement example This article implements php+mysql database random rearrangement example The sorting method can randomly read out all the data in the table once...
Statement:
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