PHP Discourse queries multiple tables in MySQL and displays them on the page

Source: Internet
Author: User
PHP statements query multiple tables in MySQL and display them on the page
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.
------to solve the idea----------------------

$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;
}
}

------to solve the idea----------------------


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

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


------to solve the idea----------------------

$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;
}
  • 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.