Php+mysql implementation of database random rearrangement instances, phpmysql instance _php tutorial

Source: Internet
Author: User
Tags php and mysql php programming

Php+mysql implementation of database random rearrangement instances, Phpmysql re-scheduling instances

In this paper, the method of random rearrangement of php+mysql database is realized, and all the data in the table can be randomly read out once and then saved to another table randomly, which achieves the function of recording random.

The main implementation code is as follows:

Copy the Code code as follows: <?php
Database connection is not written in 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;
$SQLU = "Update table prefix _info set isget=1 where id =". $rs [' id '];
mysql_query ($SQLU);
Echo ' is working on the data, currently '. $s. ' Bar ... ';
Echo ' completes all data processing and then randomly sorts once ';

I hope this article is helpful to everyone's PHP programming.

PHP reads MySQL database randomly displays a specified number of records

1) randomly remove 5 records with the SQL statement on the line;
MySQL is as follows: "SELECT * from Youtable ORDER by rand () limit 5";

2) also randomly take 5-10:
$num =rand (5,10);
$sql = "SELECT * from Youtable ORDER by rand () limit $num";

PS: Why not a PHP classification problem?

Q Php+mysql How to randomly extract several strips (for example, five data) from the data table student?

direct with SELECT * FROM Student ORDER by rand () limit 5 is not a good habit, this takes into account the size of the database problem, for example, the amount of data is more than 10W, so the efficiency of the query is very low, Resources also consume quite a lot! You can consider the combination of PHP and MySQL to achieve, such as: First use the MySQL count () to calculate the total number of student $sum_num, assigned to the variables in PHP, After in PHP more this total number of generated random number (here is whether your 5 data is a continuous 5 random numbers, or scrambled random five), if the generation of continuous data will only need to generate a less than $sum_num-5 random number can be! A limit of 5 is removed from this random number, and if there is a need to generate a random number that is scrambled, you need to generate 5 different and less than $sum_num random numbers, and then query the database based on these random numbers, and the first case uses the limit random number, 5; the second case uses the in ( Random number 1, random number 2, random number 3, random number 4, random number 5); In the case of large data volumes, such efficiency and resource consumption are much better than select * FROM Student ORDER by rand () limit 5, Of course, if the amount of data is not too much, you can use this! Just for you to popularize a knowledge! I wish you a happy study! (here for the moment you will use PHP to generate random numbers and the PHP generated here must be more than 0 of the integer type Oh, will not be the first Baidu, Google, finally can continue to give me a message)!
This is because your ID will be missing, so some methods do not apply! true techarticle Php+mysql Implement the database random rearrangement instance, phpmysql the instance of this paper realizes the method of random rearrangement of php+mysql database, can read all the data in the table randomly once ...

  • 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: 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.