首页 >后端开发 >php教程 > PHP语句在MYSQL中查询多张表 并显示在页面下

PHP语句在MYSQL中查询多张表 并显示在页面下

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原创
2016-06-13 13:12:41823浏览

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

------解决方案--------------------

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;
      }
}
<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code


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

$sql = join(' UNION ', $select);
$res = mysql_query($sql);
<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code

$table = array("abc201202","abc201203","abc201204");
$result=array();
$count = count($table)-1;//减去一个数组下标
for ($i=0;$i
                 
              
              
        
            
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn