The PHP statement is used to query multiple tables in MYSQL and displayed on the page.

Source: Internet
Author: User
The PHP statement is used to query multiple tables in MYSQL and displayed on the page. the fields of multiple tables are the same and the table names are different. For example: & nbsp; abc201202, abc201203, abc201204... the table name is created on a monthly basis. I am new users don't know how to traverse multiple tables at a time to extract MYSQL data ., The PHP statement is used to query multiple tables in MYSQL and displayed on the page.
The PHP statement is used to query multiple tables with the same fields and different table names. For example:
Abc201202, abc201203, abc201204... table name created on a monthly basis
I don't know how to traverse multiple tables at a time to extract data from MYSQL.

------ Solution --------------------
PHP code
$ Table = array ("abc201202", "abc201203", "abc201204"); $ result = array (); foreach ($ table as $ table_name) {$ SQL = "select * from ". $ table_name; $ rec = mysql_query ($ SQL); while ($ row = mysql_fetch_row ($ rec) {$ result [] = $ row ;}}
------ Solution --------------------
PHP code
$ Select = array (); $ table = array ("abc201202", "abc201203", "abc201204"); foreach ($ table as $ v) {$ select [] = "(select * from $ v)";} $ SQL = join ('join', $ select); $ res = mysql_query ($ SQL );
------ Solution --------------------
PHP code
$ Table = array ("abc201202", "abc201203", "abc201204"); $ result = array (); $ count = count ($ table)-1; // subtract an array subscript for ($ I = 0; $ I <$ count; $ I ++) {$ SQL = $ SQL. "select * from '". $ table [$ I]. "'union all";} $ SQL = $ SQL. "select * from '". $ table [$ count]. "'"; $ rec = mysql_query ($ SQL); while ($ row = mysql_fetch_row ($ rec) {$ result [] = $ row ;}

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.