Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial PHP语句在MYSQL中查询多张表 并显示在页面上

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

Jun 23, 2016 pm 01:37 PM

用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处理吧

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles