簡體   English   中英

簡化從mysql數據庫獲取數據的代碼

[英]Simplify code for taking data from mysql database

我對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個表中提取數據,則可能需要重新查看數據庫的結構。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM