2017-04-26 87 views
0

我試圖從兩個不同的數據庫合併兩個查詢(從兩個數據庫中獲取總數),並在五列表格中顯示它們兩個之間的差異。 我在一個單獨的庫完成的功能,這是我的代碼:在PHP中調用庫函數並顯示查詢結果

function getFirstTables($conn) { 
    $tables = []; 

    $q = "SELECT DISTINCT job_processing_table FROM job_type"; 

    $_tables = $conn->query($q); 
    while ($table = $_tables->fetch_assoc()) { 
     $tables []= $table["job_table"]; 
    } 

    return $tables; 
} 


/** 
* This returns a list of all call_report_* tables. 
* $conn: connection to database 
* @returns: list of tables 
*/ 
function getSecondTables($conn) { 

    $call_tables = []; 

    $r = "SHOW TABLES LIKE 'call_report_%'"; 

    $c_r_tables = $conn->query($r); 
    while ($c_r_table = $c_r_tables->fetch_assoc()) { 
     $call_tables []= $c_r_table["Tables_in_spear_reports (call_report_%)"]; 
    } 

    return $call_tables; 
} 



/** 
* This returns the constructed query for getting test data from spearlinedb 
* $conn: connection to database 
* $startDate: call start time 
* $endDate: call end time 
* @returns: constructed query as a string 
*/ 
function constructFirstQuery($conn, $startDate, $endDate) { 

    foreach(getFirstTables($conn) as $table) { 

     $sql []= "SELECT 
       number.company_id AS company_id, 
       DATE(call_start_time) AS `date`, 
       COUNT(*) AS `sub_total`, 
       '$table' AS `table` 
       FROM $table 
       LEFT JOIN number ON number.id = $table.number_id 
       WHERE $table.show IS TRUE 
       AND call_start_time BETWEEN '$startDate 00:00:00' AND '$endDate 23:59:59' 
       AND $table.processing_complete IS TRUE 
       GROUP BY `date`"; 
    } 

    $sql = implode("\n UNION \n", $sql); 
    return $sql = "SELECT company_id, `date`, 
        SUM(sub_total) AS First_total, 
        0 AS Second_total 
        FROM ($sql) AS temptable1 
        GROUP BY company_id, `date`"; 

} 


/** 
* This returns the constructed query for getting test data from reportingdb 
* $conn: connection to database 
* $startDate: call start time 
* $endDate: call end time 
* @returns: constructed query as a string 
*/ 
function construcSecondQuery($conn_2, $startDate, $endDate) { 

    foreach(getSecondTables($conn_2) as $table) { 
     $company_id = str_replace('call_report_', '', $table); 
     $sql_cr []= "SELECT 
        $company_id As company_id, 
        DATE(start_time) AS `date`, 
        COUNT(*) AS sub_total 
        FROM $table 
        WHERE $table.published = 1 
        AND start_time BETWEEN '$startDate 00:00:00' AND '$endDate 23:59:59' 
        GROUP BY `date`"; 
     } 


     $sql_cr = implode("\n UNION \n", $sql_cr); 
     return $sql_cr = "SELECT company_id,`date`, 0 AS First_total, SUM(sub_total) AS Second_total FROM ($sql_cr) AS tempTable GROUP BY company_id, `date`"; 

我想顯示二者的合併有以下的列表:COMPANY_ID,日期,總,report_total,差。我遇到的問題是從單獨的庫調用函數。這是我寫的代碼:

$sql = ""; 


echo constructFirstQuery($conn, $startDate, $endDate);; 
echo constructSecondQuery($conn_2, $startDate, $endDate); 

$result_first = constructFirstQuery($conn, $startDate, $endDate); 
$result_first = $conn->query($sql) or die($conn->error); 

$result_cr =constructSecondQuery($conn_2, $startDate, $endDate); 
$result_cr = $conn_2->query($sql_cr) or die($conn_2->error); 


$total_results = array(); 
while ($row = $result_first->fetch_assoc()) { 
    $total_results[$row['company_id']] = $row; 
} 

while ($row = $result_cr->fetch_assoc()) { 
    if(!isset($total_results[$row['company_id']])) { 
     $total_results[$row['company_id']] = $row; 
    } else { 
     $total_results[$row['company_id']]['Reporting_total'] = $row['Reporting_total']; 
    } 
} 

if ($total_results) { 
    echo"<TABLE><TR><TH>Company ID</TH> 
     <TH>Date</TH> 
     <TH>Spearlinedb Total</TH> 
     <TH>Reportingdb Total</TH> 
     <TH>Difference</TH></TR>"; 



    foreach($total_results as $row) { 

     $first_total = $row['First_total']; 
     $second_total = $row['Second_total']; 
     $difference = ($first_total - $second_total); 
     echo"<TR><TD>". $row["company_id"]. "</TD>"; 
     echo"<TD>". $row["date"]. "</TD>"; 
     echo"<TD>". $row["First_total"]. "</TD>"; 
     echo"<TD>". $row["Second_total"]. "</TD>"; 
     echo"<TD>". $difference . "</TD></TR>"; 
    } 
    echo"</TABLE>"; 
} else { 
    echo "0 results"; 
} 

此刻,當我運行的代碼,我收到此錯誤:的mysqli ::查詢():在....空查詢。這些函數本身運行良好,並將查詢打印爲字符串,但我似乎無法在主代碼中調用它們,並讓它們按我的預期返回查詢。任何人都可以用這個指向正確的方向嗎?我仍然在學習PHP,所以任何幫助表示讚賞。

+0

'空查詢in'猜線下賽,耶! –

+0

@u_mulder那我能拿獎嗎? – RiggsFolly

+0

當然可以。 –

回答

0

由於混淆了變量名稱,因此您沒有將構建的查詢傳遞給​​方法。

請參見下面的編輯

$sql = ""; 

//$result_first = constructFirstQuery($conn, $startDate, $endDate); 
$sql = constructFirstQuery($conn, $startDate, $endDate); 
$result_first = $conn->query($sql) or die($conn->error); 

//$result_cr = constructSecondQuery($conn_2, $startDate, $endDate); 
$sql = constructSecondQuery($conn_2, $startDate, $endDate); 
$result_cr = $conn_2->query($sql) or die($conn_2->error); 
+0

請問爲什麼您使用$ sql而不是$ sql_cr?謝謝 – seb

+0

因爲一旦你加載了'$ sql'並將它傳遞給第一個' - >查詢($ sql)',它就完成了它的功能並且不會再被使用。因此,您可以將其重新用於第二個查詢。 – RiggsFolly

+0

您還應該考慮在交易中包裝這3個更新。因此,如果任何一個失敗,以前的所有更改都會回滾,從而使數據庫保持乾淨狀態。 – RiggsFolly

相關問題