我试图从两个不同的数据库合并两个查询(从两个数据库中获取总数),并在五列表格中显示它们两个之间的差异。 我在一个单独的库完成的功能,这是我的代码:在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,所以任何帮助表示赞赏。
'空查询in'猜线下赛,耶! –
@u_mulder那我能拿奖吗? – RiggsFolly
当然可以。 –