在PHP中调用库中的函数并显示查询结果

时间:2017-04-26 11:17:38

标签: php mysql database function

我尝试合并来自两个不同数据库的两个查询(从两个数据库中获取总数),并在五列表中显示它们之间的差异。 我在一个单独的库中完成了这些功能,这是我的代码:

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,date,total,report_total,difference。我遇到问题的地方是从单独的库中调用函数。这是我写的代码:

$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 :: query():空查询.... 。这些函数可以自行运行并将查询打印为字符串,但我似乎无法在主代码中调用它们并让它们按照我的预期返回查询。请问有人能指出我正确的方向吗?我还在学习PHP,所以感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您没有将构建的查询传递给->query()方法,因为您混淆了变量名称。

请参阅下面的编辑

$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);