PHP statements query multiple tables in MySQL and display them on the page

Source: Internet
Author: User
The fields that query multiple table tables with the PHP statement are the same, and the table names are different. For example:
abc201202,abc201203,abc201204 ... Table name by Month table
I don't know how to walk through more than one table at a time to extract data from MySQL.


Reply to discussion (solution)

Use the For loop to include the SQL statement in the inside to repeat, do not know whether it can make sense.

Take the data multiple times and process it.

$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);
......



The data you have taken out is not paged?

PHP Code


$table = Array ("abc201202", "abc201203", "abc201204");
$result =array ();
foreach ($table as $table _name) {
$sql = "SELECT * from". $table _name;
$rec =mysql_query ($sql);
......


You can also traverse data, but not paging data?

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


Your code I tried, echo $sql; output is: SELECT * from ' abc201202 ' UNION ALL SELECT * from ' abc201203 ' UNION ALL SELECT * from ' abc201204 ' UNION ALL SELECT * from '

I modified the next output into: SELECT * FROM twebmailaction_201208 UNION ALL SELECT * FROM twebmailaction_201209 UNION ALL SELECT * FROM TWEBM AILACTION_201210 UNION ALL

I use echo $num = Mysql_num_rows ($sql); No results.

PHP Code


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

$sql = Join (' UNION ', $select);
$res = mysql_query ...


I tried it with your code. You can query the number of data bars of several tables and are right.

But do not know how to show the data frame has, pagination seems to be right, there is no database field content,

There is a click on the next page, prompt: Warning:mysql_fetch_array (): supplied argument is not a valid MySQL result resource in F:\www_local\G03\lis T5.php on line 88 and the frame that shows the data is gone.
Novice Seeking Education

$table = Array ("abc201202", "abc201203", "abc201204"), $result =array (), $count = count ($table) -1;//minus an array subscript 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;//minus an array subscript
for ($i =0; $i < $count; $i + +) {
$sql = $sql. "SELECT * from". $table [$i]. "' Unio ...


Now it's ready for you. There is a problem:

1, paging seems to be a bit of a problem: $sql = $sql. "SELECT * from". $table [$count]. "'". "Limit $page $pagesize"; for paging I added limit. Does this add up right?

2. Traversal loop I changed. The query displays the value of a field:
while ($row =mysql_fetch_row ($rec)) {
echo " ". $row [Username];
}
The output is just a lot of straight lines, no field values?


Limit $page $pagesize changed to limit $page, $pagesize try

Limit $page $pagesize changed to limit $page, $pagesize try


That's not the problem. I have this statement in front: $page. = ', ';

Now the main is to display the contents of the field do not know how to assign value?

PHP Code


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

$sql = Join (' UNION ', $select);
$res = mysql_query ...



The implementation of the specified field display is the difference! Do not know how to traverse the specified field: for example while ($row = (mysql_fetch_row ($res))) {
echo $row [user];
} pagination shows user this field

PHP Code


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

$sql = Join (' UNION ', $select);
$res = mysql_query ...


Excuse me for example: while ($row = (mysql_fetch_row ($res))) {
echo $row [user];
A page like this shows how the user field does not output

This is not good, still use PHP processing it

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.