[英]PHP Mysqli->query() returns false for SELECT INNER JOIN, Mysqli->error returns empty
EDIT: Added current data in use 编辑:添加当前正在使用的数据
I have a small functions file I've created as a database interface to be called for handling queries in a simple manner. 我有一个小的函数文件,它已经创建为数据库接口,可以调用它以简单的方式处理查询。
// 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
);
}
In most cases I'll build an SQL query somewhere else and pass it to this runSQLQuery function, then echo the response as JSON for my web-app. 在大多数情况下,我将在其他地方构建一个SQL查询,并将其传递给此runSQLQuery函数,然后将响应作为JSON回显我的Web应用程序。
I've had no issues with simple SELECT, UPDATE, DELETE commands, all have worked successfully. 我对简单的SELECT,UPDATE,DELETE命令没有任何问题,所有这些都已成功运行。 However I want to implement something slightly more complex for one function, a SELECT INNER JOIN query. 但是我想为一个函数SELECT INNER JOIN查询实现稍微复杂一点的东西。 The function building this query and executing is: 构建此查询并执行的函数是:
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;
}
For some reason running this specific query returns false. 由于某种原因,运行此特定查询将返回false。 The JSON output I receive on my web app is 我在网络应用上收到的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;"
}
If I run this query directly I actually get the correct results, several rows of data with a CONCAT column of packages as 1,2,3 as expected. 如果直接运行此查询,我实际上会得到正确的结果,数据的几行与预期的包的CONCAT列分别为1,2,3。
Database structure: 数据库结构:
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
);
Output of query run directly: 查询的输出直接运行:
+----+-------------------------------------------------------------------------------+---------------------+------+-----------+--------+--------+----------+ | 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 | +----+-------------------------------------------------------------------------------+---------------------+------+-----------+--------+--------+----------+
If the query has no issue, and other queries work within my function, why is this specific SELECT INNER JOIN query failing with no error? 如果查询没有问题,并且其他查询在我的函数中起作用,那么为什么此特定的SELECT INNER JOIN查询失败而没有错误?
EDIT: 编辑:
nr_jobs data: 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 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 | +----+------------------+----------+
答:上一个功能包含一个错字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.