Php + mysql implements random database rearranging instances and phpmysql rearranging instances

Source: Internet
Author: User
Tags php and mysql

Php + mysql implements random database rearranging instances and phpmysql rearranging instances

In this example, we have implemented the php + mysql database random shuffling method. We can randomly read all the data in the table and save it to another table. This achieves the random record function.

The main implementation code is as follows:

Copy codeThe Code is as follows: <? Php
// The database connection will not be 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 ['ss'];
$ Typeid = $ rs ['typeid'];
$ Isget = $ rs ['isget'];
$ SQL = "insert into table prefix _ info_bak (id, table prefix, typeid, isget) values ('$ id',' $ ss', '$ typeid ', '$ isget ')";
Mysql_query ($ SQL );
Echo $ SQL;
// Exit;
$ Sqlu = "update table prefix _ info set isget = 1 where id =". $ rs ['id'];
Mysql_query ($ sqlu );
}
Echo '<meta http-equiv = "refresh" content = "0; url = rand. php? S = '. ($ s + 50).' & e = 50 "> processing data. The current value is '. $ s ......';
}
Else
{
Echo 'complete all data processing <a href = rand. php> and then sort the data randomly. </a> ';
}
?>

I hope this article will help you with PHP programming.


Php randomly displays a specified number of records when reading mysql Databases

1) randomly retrieve 5 Records and use SQL statements;
Mysql is as follows: "select * from youtable order by rand () limit 5 ";

2) randomly fetch 5-10 results:
$ Num = rand (5, 10 );
$ SQL = "select * from youtable order by rand () limit $ num ";

Ps: Why is it not a php classification problem?

How can I randomly extract several (for example, five data records) from the data table student in php + mysql?

Directly Using select * from student order by rand () limit 5 is not a good habit. This should take into account the size of the database. For example, if the data volume is more than 10 million, in this way, the query efficiency is very low and the resource consumption is quite high! You can consider combining php and mysql. For example, use the count () of mysql to calculate the total number of student $ sum_num and assign the value to the variable in php, and then generate a random number with this total number in php (whether your five pieces of data are five consecutive random numbers or five random digits that are disrupted ), here if you generate continuous data, you only need to generate a random number less than $ sum_num-5! You can extract 5 random numbers based on the random number. If you want to generate a random number that is disordered here, you need to generate 5 random numbers that are different and less than $ sum_num, finally, the random numbers are queried in the database. in the first case, the limit random number, 5, and in (Random Number 1, Random Number 2, random number 3, random number 4, random Number 5); in the case of a large amount of data, such efficiency and resource consumption are much better than select * from student order by rand () limit 5, of course, this can be used if there is not much data! Just give you a general knowledge! Happy learning! (For the time being, I think you will use php to generate a random number and the random number generated by php must be an integer greater than 0. If not, Baidu, google, finally, you can leave a message for me )!
Some methods are not applicable because your id is missing! I will not list them one by one!

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.