Query multiple tables in MYSQL and display them on the page.-PHP Tutorial

Source: Internet
Author: User
The PHP statement queries multiple tables in MYSQL and is 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.


Reply to discussion (solution)

The SQL statement is included in the for loop for repeated execution. I don't know if it works.

Retrieve data multiple times before processing

$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;      }}

PHP code


$ Table = array ("abc201202", "abc201203", "abc201204 ");
$ Result = array ();
Foreach ($ table as $ table_name ){
$ SQL = "select * from". $ table_name;
$ Rec = mysql_query ($ SQL );
......



The data you obtained in this way cannot be paged?

PHP code


$ Table = array ("abc201202", "abc201203", "abc201204 ");
$ Result = array ();
Foreach ($ table as $ table_name ){
$ SQL = "select * from". $ table_name;
$ Rec = mysql_query ($ SQL );
......


In this way, you can also traverse the data, but not the paging data?

$select = array();$table = array("abc201202","abc201203","abc201204");foreach($table as $v) {   $select[] = "(select * from $v)";}$sql = join(' UNION ', $select);$res = mysql_query($sql);

$table = array("abc201202","abc201203","abc201204");$result=array();$count = count($table);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;      }

PHP code

$ Table = array ("abc201202", "abc201203", "abc201204 ");
$ Result = array ();
$ Count = count ($ table );
For ($ I = 0; $ I <$ count; $ I ++ ){
$ SQL = $ SQL. "select * from '". $ table [$ I]. "'union all ";
}
$ S ......


I tried your CODE, echo $ SQL; the output is: select * from 'abc201202 'union all select * from 'abc201203' union all select * from 'abc201204 'union all select * from''

I changed the output to select * from twebmailaction_201208 union all select * from twebmailaction_201209 union all select * from twebmailaction_201210 union all

I used echo $ num = mysql_num_rows ($ SQL); no result.

PHP code


$ Select = array ();
$ Table = array ("abc201202", "abc201203", "abc201204 ");
Foreach ($ table as $ v ){
$ Select [] = "(select * from $ v )";
}

$ SQL = join ('join', $ select );
$ Res = mysql_query ......


With your CODE, I tried to query the number of data records and correct values of several tables.

But I don't know what's going on. it shows that the data framework has a correct page, that is, there is no database field content,

Also, after clicking the next page, the prompt is: Warning: mysql_fetch_array (): supplied argument is not a valid MySQL result resource in F: \ www_local \ G03 \ list5.php on line 88 and no data frame is displayed.
New users seek education

$ 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 ;}

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]. "'unio ......


Now you can. Some other problems:

1. there seems to be some problems with Paging: $ SQL = $ SQL. "select * from '". $ table [$ count]. "'". "limit $ page $ pagesize"; I added limit for paging. Is this correct?

2. I have changed the traversal loop. the query displays the value of a field:
While ($ row = mysql_fetch_row ($ rec )){
Echo" ". $ Row [Username];
}
There are only a lot of straight lines in the output, and there is no field value?


Limit $ page $ change pagesize to limit $ page, $ pagesize try

Limit $ page $ change pagesize to limit $ page, $ pagesize try


This is not the problem. I have the preceding statement: $ page. = ',';

I don't know how to assign values to the displayed fields?

PHP code


$ Select = array ();
$ Table = array ("abc201202", "abc201203", "abc201204 ");
Foreach ($ table as $ v ){
$ Select [] = "(select * from $ v )";
}

$ SQL = join ('join', $ select );
$ Res = mysql_query ......



The display of the specified field is poor! I don't know how to traverse the specified field: for example, while ($ row = (mysql_fetch_row ($ res ))){
Echo $ row [user];
} The user field is displayed on pages.

PHP code


$ Select = array ();
$ Table = array ("abc201202", "abc201203", "abc201204 ");
Foreach ($ table as $ v ){
$ Select [] = "(select * from $ v )";
}

$ SQL = join ('join', $ select );
$ Res = mysql_query ......


For example, while ($ row = (mysql_fetch_row ($ res ))){
Echo $ row [user];
} Why is no output of the user field displayed on pages like this?

This is not good. use php to handle it.

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.