php - 简化从mysql数据库取数据的代码

标签 php mysql arrays fetch

我对 php 和 mysql 有点陌生,我创建了这段代码来从多个表(252 个表)中获取数据。

问题是它是一个巨大的代码,我想知道是否有任何方法可以简化它:

<?php

//Database connect:
$dbconnect = new MySQLi("localhost","user","pass","generaldata");
if ($dbconnect->connect_errno){die("Connection failed: " . $dbgeneral->connect_error);}


//Get data from 252 tables:
$res1 = $dbconnect->query("SELECT column1, column2, column3, column4, column5 FROM table1 ORDER BY Id DESC LIMIT 1"); 
$row1 = $res1->fetch_assoc();
$table1col1= $row1["column1"]; $table1col2= $row1["column2"]; $table1col3= $row1["column3"]; $table1col4= $row1["column4"]; $table1col5= $row1["column5"];

$res2 = $dbconnect->query("SELECT column1, column2, column3, column4, column5 FROM table2 ORDER BY Id DESC LIMIT 1"); 
$row2 = $res2->fetch_assoc();
$table2col1= $row1["column1"]; $table2col2= $row1["column2"]; $table2col3= $row1["column3"]; $table2col4= $row1["column4"]; $table2col5= $row1["column5"];


$res3 = $dbconnect->query("SELECT column1, column2, column3, column4, column5 FROM table3 ORDER BY Id DESC LIMIT 1");
$row3 = $res3->fetch_assoc(); 
$table3col1= $row1["column1"]; $table3col2= $row1["column2"]; $table3col3= $row1["column3"]; $table3col4= $row1["column4"]; $table3col5= $row1["column5"];


$res4 = $dbconnect->query("SELECT column1, column2, column3, column4, column5 FROM table4 ORDER BY Id DESC LIMIT 1"); 
$row4 = $res4->fetch_assoc();
$table4col1= $row1["column1"]; $table4col2= $row1["column2"]; $table4col3= $row1["column3"]; $table4col4= $row1["column4"]; $table4col5= $row1["column5"];


$res5 = $dbconnect->query("SELECT column1, column2, column3, column4, column5 FROM table5 ORDER BY Id DESC LIMIT 1"); 
$row5 = $res5->fetch_assoc();
$table5col1= $row1["column1"]; $table5col2= $row1["column2"]; $table5col3= $row1["column3"]; $table5col4= $row1["column4"]; $table5col5= $row1["column5"];

// This goes on until 252 tables. Every table has a unique name.

// Creating an 5 array with the results:
$Array1 = array('table1col1' => $table1col1, 'table2col1' => table3col1, 'table3col1' => table3col1, 'table4col1' => table4col1, 'table5col1' => table5col1, [...] 'table252col1' => table252col1);

$Array1 = array('table1col1' => $table1col1, 'table2col1' => table3col1, 'table3col1' => table3col1, 'table4col1' => table4col1, 'table5col1' => table5col1, [...] 'table252col1' => table252col1);

$Array2 = array('table1col2' => $table1col2, 'table2col2' => table3col2, 'table3col2' => table3col2, 'table4col2' => table4col2, 'table5col2' => table5col2, [...] 'table252col2' => table252col2);

$Array3 = array('table1col3' => $table1col3, 'table2col3' => table3col3, 'table3col3' => table3col3, 'table4col3' => table4col3, 'table5col3' => table5col3, [...] 'table252col3' => table252col3);

$Array4 = array('table1col4' => $table1col4, 'table2col4' => table3col4, 'table3col4' => table3col4, 'table4col4' => table4col4, 'table5col4' => table5col4, [...] 'table252col4' => table252col4);

$Array4 = array('table1col5' => $table1col5, 'table2col5' => table3col5, 'table3col5' => table3col5, 'table4col5' => table4col5, 'table5col5' => table5col5, [...] 'table252col5' => table252col5);

//Then I filter the arrays to remove null results.
$Array1 = array_filter($Array1); $Array2 = array_filter($Array2); $Array3 = array_filter($Array3); $Array4 = array_filter($Array4); $Array5 = array_filter($Array5);

//Finally I sort the array to order the values
$Array1 = arsort($Array1); $Array2 = arsort($Array2); $Array3 = arsort($Array3); $Array4 = arsort($Array4); $Array5 = arsort($Array5);

非常感谢任何帮助

最佳答案

可能有一个使用 MySQL 命令的解决方案,但不知道数据如何链接在一起,如果有的话,这是一种缩短脚本占用的行数的方法:

<?php

//Database connect:
$dbconnect = new MySQLi("localhost","user","pass","generaldata");
if ($dbconnect->connect_errno){die("Connection failed: " . $dbgeneral->connect_error);}

$data = array();
$tableNames = array(
    'table1',
    'table2',
    'table3',
    // other table names
    'table252'
);
$numTables = count($tableNames);

for($i = 0; $i < $numTables; $i++){
    $query = $dbconnect->query("SELECT column1, column2, column3, column4, column5 FROM ".$tableNames[$i]." ORDER BY Id DESC LIMIT 1"); 
    $data[] = $query->fetch_assoc();
}

如果每个表都有不同的列名,那么我可以更改脚本以适应它。

注意: 如果您需要一次从 252 个表中提取数据,您可能需要重新审视您构建数据库的方式。

关于php - 简化从mysql数据库取数据的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33603434/

相关文章:

python - mysql.connector SELECT 查询是并发运行还是顺序运行?

PHP - 使用另一个表插入数据库

java - 用下一个最大的元素替换每个元素(按升序排列,不替换为 -1)

php - 在远程机器 php 上运行 bash 脚本

php - 为什么我不能从 android 获取 php 中的值?

php - WordPress 自定义 PHP 页面重写规则

mysql - 当我尝试使用 docker-compose 为 Nodejs 和 mysql 应用程序运行 docker 容器时,出现端口使用错误

arrays - Postgres - 通过 SQL 插入一个 varchar 数组

c - (*twod)[3] 与 *(twod)[3] C 指针

php - 使用mysql和php下载pdf文件