PHP语句在MYSQL中查询多张表 并显示在页面上
用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; }}
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);
$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……
你的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; }
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处理吧

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

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,

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 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? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

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

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�...

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.
