Home > Backend Development > PHP Tutorial > PHP话语在MYSQL中查询多张表 并显示在页面上

PHP话语在MYSQL中查询多张表 并显示在页面上

WBOY
Release: 2016-06-13 12:17:24
Original
885 people have browsed it

PHP语句在MYSQL中查询多张表 并显示在页面上
用PHP语句实现查询多张表 表的字段是相同的、表名不同。例如:
           abc201202,abc201203,abc201204...表名按月建的表
我新手不知道怎么一次遍历多张表,提取MYSQL中的数据。    
------解决思路----------------------

<br />$table = array("abc201202","abc201203","abc201204");<br />$result =array();<br />foreach($table as $table_name) {<br />      $sql = "select * from ".$table_name;<br />      $rec =mysql_query($sql);<br />      while($row=mysql_fetch_row($rec)) {<br />           $result[] = $row;<br />      }<br />}<br />
Copy after login

------解决思路----------------------
<br /><br />$select = array();<br />$table = array("abc201202","abc201203","abc201204");<br />foreach($table as $v) {<br />   $select[] = "(select * from $v)";<br />}<br /><br />$sql = join(' UNION ', $select);<br />$res = mysql_query($sql);<br /><br />
Copy after login

------解决思路----------------------
<br />$table = array("abc201202","abc201203","abc201204");<br />$result=array();<br />$count = count($table)-1;//减去一个数组下标<br />for ($i=0;$i<$count;$i++){<br />      $sql = $sql." select * from '".$table[$i]."' union all";<br />}<br />$sql=$sql." select * from '".$table[$count]."'";<br />$rec =mysql_query($sql);<br />      while($row=mysql_fetch_row($rec)) {<br />           $result[] = $row;<br />      }<br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template