【发布时间】:2016-02-09 18:18:41
【问题描述】:
我对 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);
任何帮助将不胜感激
【问题讨论】:
-
您可能可以使用
UNION ALL或JOIN,但如果不知道您要检索什么,就很难判断。 -
1) 所有表都有相同的列名? 2)您真的需要将值存储在变量中还是可以将它们存储在关联数组中,即 $result['table1col1']。 ?
-
查看 SQL 语句和您提到的表数(252 !!)我想说您的数据库模式设计也可能有问题。您是否有 252 个表用于同一实体的 252 个实例?
-
Skriptotajs 1) 是的,这些表具有相同的列名。 2)必须存储值,因为我必须检查 if( value = -50 ){ value = null; }。然后我将它们放入数组中,然后过滤它以删除空值
-
好的,我决定重组数据库