编辑:添加了正在使用的当前数据
我已经创建了一个小的函数文件作为数据库接口,可以调用它以简单的方式处理查询。
// Run SQL query
function runSQLQuery($sql) {
// Open DB
$db = connectToDB();
if( $db->connect_errno ) {
$data['response'] = false;
$data['error_code'] = $db->connect_errno;
$data['error'] = $db->connect_error;
return $data;
}
// Run & return query
$response = $db->query($sql);
if( is_bool($response) ) {
$response = ($response) ? 'true' : 'false';
}
switch($response) {
case "true":
$data['raw'] = $response;
$data['response'] = true;
$data['error'] = null;
$data['id'] = $db->insert_id;
return $data;
case "false":
$data['raw'] = $response;
$data['response'] = false;
$data['error_code'] = $db->errno;
$data['error'] = $db->error;
return $data;
default:
$data['raw'] = $response;
$data['response'] = true;
$data['error'] = null;
if( $response->num_rows > 0) {
while( $row = $response->fetch_assoc() ) {
$data['data'][] = $row;
}
}
else {
$data['data'] = null;
}
return $data;
}
}
// Open database connection
function connectToDB() {
// Connect to DB
return new mysqli(
DB_SERVER,
DB_USER,
DB_PASS,
DB_NAME
);
}
在大多数情况下,我将在其他地方构建一个SQL查询并将其传递给此runSQLQuery函数,然后将响应作为JSON回显我的Web应用程序。
我对简单的SELECT,UPDATE,DELETE命令没有任何问题,都可以成功运行。但是我想为一个函数SELECT INNER JOIN查询实现稍微复杂一点的东西。构建此查询并执行的函数是:
public function get($args) {
extract($args);
// Get event
if(isset($userId)) {
$sql =
"SELECT j.id, j.event_address as address, j.event_datetime as datetime, j.event_note as note, j.event_price as price, j.client_id as userId, j.client_card_id as cardId, GROUP_CONCAT(p.event_package_id) as packages
FROM nr_jobs as j
INNER JOIN nr_job_packages as p ON p.event_id = j.id
WHERE j.client_id = $userId
GROUP BY j.id;";
}
if(isset($jobId)) {
$sql =
"SELECT j.id, j.event_address as address, j.event_datetime as datetime, j.event_note as note, j.event_price as price, j.client_id as userId, j.client_card_id as cardId, GROUP_CONCAT(p.event_package_id) as packages
FROM nr_jobs as j
INNER JOIN nr_job_packages as p ON p.event_id = j.id
WHERE j.id = $jobId
GROUP BY j.id;";
}
$events = runSQLQuery($sql);
$events["sql"] = $sql;
return $events;
}
由于某种原因,运行此特定查询将返回false。我在网络应用上收到的JSON输出是
{
"raw": "false",
"response": false,
"error_code": 0,
"error": "",
"sql": "SELECT j.id, j.event_address as address, j.event_datetime as datetime, j.event_note as note, j.event_price as price, j.client_id as userId, j.client_card_id as cardId, GROUP_CONCAT(p.event_package_id) as packages FROM nr_jobs as j INNER JOIN nr_job_packages as p ON p.event_id = j.id WHERE j.client_id = 1 GROUP BY j.id;"
}
如果直接运行此查询,我实际上会得到正确的结果,几行数据的包的CONCAT列为预期的1,2,3。
数据库结构:
CREATE TABLE IF NOT EXISTS nr_jobs(
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
event_address TEXT NOT NULL,
event_datetime DATETIME NOT NULL,
event_note TEXT,
event_price DECIMAL(13,4) NOT NULL,
client_id BIGINT NOT NULL,
client_card_id BIGINT NOT NULL,
FOREIGN KEY (client_id) REFERENCES nr_clients(id),
FOREIGN KEY (client_card_id) REFERENCES nr_payment_cards(id)
);
CREATE TABLE IF NOT EXISTS nr_job_packages(
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
event_package_id BIGINT NOT NULL,
event_id BIGINT NOT NULL,
FOREIGN KEY (event_id) REFERENCES nr_jobs(id) ON DELETE CASCADE,
FOREIGN KEY (event_package_id) REFERENCES nr_packages(id) ON DELETE CASCADE
);
查询输出直接运行:
+----+-------------------------------------------------------------------------------+---------------------+------+-----------+--------+--------+----------+ | id | address | datetime | note | price | userId | cardId | packages | +----+-------------------------------------------------------------------------------+---------------------+------+-----------+--------+--------+----------+ | 1 | SIM HQ LT3.02A, 461 Clementi Rd, Singapore, South West 59, Singapore | 2018-11-28 10:38:00 | | 320.0080 | 1 | 3 | 2,3 | | 2 | Tay Eng Soon Library, Blk A, SIM HQ, Singapore, South West 59, Singapore | 2018-11-28 10:42:00 | | 320.0080 | 1 | 3 | 2,3,4 | | 3 | SIM HQ Corporate Office, 461 Clementi Rd, Singapore, South West 59, Singapore | 2018-11-28 10:43:00 | | 1020.0000 | 1 | 3 | 1,2,3,4 | | 4 | L.A. City DOT, Los Angeles, California 90012, United States | 2018-11-28 12:15:00 | | 860.0000 | 1 | 4 | 2,3,4 | +----+-------------------------------------------------------------------------------+---------------------+------+-----------+--------+--------+----------+
如果查询没有问题,并且其他查询在我的函数中起作用,为什么此特定的SELECT INNER JOIN查询失败而没有错误?
编辑:
nr_jobs数据:
SELECT * FROM nr_jobs;
+----+-------------------------------------------------------------------------------+---------------------+------------+-------------+-----------+----------------+ | id | event_address | event_datetime | event_note | event_price | client_id | client_card_id | +----+-------------------------------------------------------------------------------+---------------------+------------+-------------+-----------+----------------+ | 1 | SIM HQ LT3.02A, 461 Clementi Rd, Singapore, South West 59, Singapore | 2018-11-28 10:38:00 | | 320.0080 | 1 | 3 | | 2 | Tay Eng Soon Library, Blk A, SIM HQ, Singapore, South West 59, Singapore | 2018-11-28 10:42:00 | | 320.0080 | 1 | 3 | | 3 | SIM HQ Corporate Office, 461 Clementi Rd, Singapore, South West 59, Singapore | 2018-11-28 10:43:00 | | 1020.0000 | 1 | 3 | | 4 | L.A. City DOT, Los Angeles, California 90012, United States | 2018-11-28 12:15:00 | | 860.0000 | 1 | 4 | +----+-------------------------------------------------------------------------------+---------------------+------------+-------------+-----------+----------------+
nr_job_packages
SELECT * FROM nr_job_packages;
+----+------------------+----------+ | id | event_package_id | event_id | +----+------------------+----------+ | 1 | 2 | 1 | | 2 | 3 | 1 | | 3 | 2 | 2 | | 4 | 3 | 2 | | 5 | 4 | 2 | | 6 | 1 | 3 | | 7 | 2 | 3 | | 8 | 3 | 3 | | 9 | 4 | 3 | | 10 | 2 | 4 | | 11 | 3 | 4 | | 12 | 4 | 4 | +----+------------------+----------+
答案 0 :(得分:0)
答案:上一个功能包含一个错字。