简化从mysql数据库获取数据的代码

时间:2015-11-09 06:15:41

标签: 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);

任何帮助都将受到高度赞赏

1 个答案:

答案 0 :(得分:2)

可能有一个使用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个表,您需要一次提取数据,您可能需要重新访问您的数据库结构。