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

WBOY
Release: 2016-06-23 13:37:18
Original
990 people have browsed it

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


回复讨论(解决方案)

用for循环把sql语句包含在里面重复执行,不知道能不能行得通

多次取数据再进行处理吧

$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;      }}
Copy after login

PHP code


$table = array("abc201202","abc201203","abc201204");
$result =array();
foreach($table as $table_name) {
$sql = "select * from ".$table_name;
$rec =mysql_query($sql);
……



你这样取出来的数据 不能分页啊?

PHP code


$table = array("abc201202","abc201203","abc201204");
$result =array();
foreach($table as $table_name) {
$sql = "select * from ".$table_name;
$rec =mysql_query($sql);
……


你这样做可也遍历数据 ,但不能分页数据啊?

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

$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;      }
Copy after login

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……


你的CODE我试了下,echo $sql;输出是:select * from 'abc201202' union all select * from 'abc201203' union all select * from 'abc201204' union all select * from ''

我修改了下输出变成了:select * from twebmailaction_201208 union all select * from twebmailaction_201209 union all select * from twebmailaction_201210 union all

我用echo $num = mysql_num_rows($sql); 没有结果。

PHP code


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

$sql = join(' UNION ', $select);
$res = mysql_query……


用你的CODE我试了一下可以查询 几张表的数据条数和是对的。

但不知道怎么回事,显示数据框架有,分页好像是对的,就是没有数据库字段内容,

还有就是点击下一页后,提示:Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:\www_local\G03\list5.php on line 88 并且显示数据的框架都没有了。
新手求教育

$table = array("abc201202","abc201203","abc201204");$result=array();$count = count($table)-1;//减去一个数组下标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;      }
Copy after login

PHP code

$table = array("abc201202","abc201203","abc201204");
$result=array();
$count = count($table)-1;//减去一个数组下标
for ($i=0;$i       $sql = $sql." select * from '".$table[$i]."' unio……


现在可以可以了。还有点问题:

1、分页好像还有点问题:$sql=$sql." select * from '".$table[$count]."'"." limit $page $pagesize";为了分页我加了limit。这样加对吗?

2、遍历循环我变了下,查询显示一个字段的值:     
          while($row=mysql_fetch_row($rec)){
         echo "
".$row[Username];
        }
输出就只有很多直线,没有字段的值?


limit $page $pagesize改成limit $page,$pagesize试试

limit $page $pagesize改成limit $page,$pagesize试试


不是这个问题。我前面有这个语句:$page.=','; 

现在主要是显示字段内容不知道怎么赋值?

PHP code


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

$sql = join(' UNION ', $select);
$res = mysql_query……



就差指定字段显示的实现了!不知道怎么遍历指定字段:例如while($row=(mysql_fetch_row($res))){
                                            echo $row[user];            
                                            }分页显示user这个字段

PHP code


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

$sql = join(' UNION ', $select);
$res = mysql_query……


请问例如:while($row=(mysql_fetch_row($res))){
   echo $row[user];   
  }像这样分页显示user这个字段怎么没有输出

这样不好的,还是用php处理吧

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