Rumah >pangkalan data >tutorial mysql >Bagaimanakah saya boleh mengembalikan berbilang data respons dalam satu respons menggunakan PHP dan PDO?
Kembalikan Berbilang Data Respons dalam Satu Respons
Kod yang disediakan cuba untuk mendapatkan berbilang rekod daripada pangkalan data dan mengembalikannya dalam satu respons. Walau bagaimanapun, disebabkan penggunaan fetchAll yang salah, ia tidak dapat menggabungkan hasil dan mengembalikan output yang diingini.
Kod berikut menyediakan penyelesaian yang diperbetulkan:
<code class="php">try { $dbAdapter = new DbAdapter(); $connection = $dbAdapter->connect(); // User IDs to be fetched. $userIds = [1, 2]; // The sql statement - it will be prepared. $sql = 'SELECT users.id AS userid, users.name AS username, subjects.id AS subject_id, subjects.name AS subject_name, subjects.points AS active_points, IFNULL(SUM(subjects.points), 0) AS total_points, ( SELECT IFNULL(SUM(points), 0) FROM subjects WHERE userid = users.id AND semester = 1 ) AS semester_1_points, ( SELECT IFNULL(SUM(points), 0) FROM subjects WHERE userid = users.id AND semester = 2 ) AS semester_2_points, ( SELECT IFNULL(SUM(points), 0) FROM subjects WHERE userid = users.id AND semester = 3 ) AS semester_3_points FROM tbsubjects AS subjects LEFT JOIN tbusers AS users ON users.id = subjects.userid WHERE subjects.userid IN (:userIds) GROUP BY subjects.userid ORDER BY subjects.time DESC'; // The input parameters list for the prepared sql statement. $bindings = array( ':userIds' => $userIds, ); // Prepare and validate the sql statement. $statement = $connection->prepare($sql); if (!$statement) { throw new UnexpectedValueException('The sql statement could not be prepared!'); } // Bind the input parameters to the prepared statement. foreach ($bindings as $key => $value) { $bound = $statement->bindValue( getInputParameterName($key), $value, getInputParameterDataType($value) ); if (!$bound) { throw new UnexpectedValueException('An input parameter can not be bound!'); } } // Execute the prepared statement. $executed = $statement->execute(); if (!$executed) { throw new UnexpectedValueException('The prepared statement could not be executed!'); } // Fetch users list - array of objects. $users = $statement->fetchAll(PDO::FETCH_OBJ); if ($users === FALSE) { throw new UnexpectedValueException('Fetching users list failed!'); } // Close connection. $connection = NULL; // Handle results. if (empty($users)) { $response->getBody()->write( '{ "error": { "message":"Invalid" } }' ); } else { $response->getBody()->write(json_encode($users)); } } catch (PDOException $exc) { echo $exc->getMessage(); // $logger->log($exc); exit(); } catch (Exception $exc) { echo $exc->getMessage(); // $logger->log($exc); exit(); }</code>
Dalam penyelesaian ini:
Akibatnya, kod ini mengambil berbilang baris daripada pangkalan data dengan betul dan menggabungkannya menjadi satu respons, yang sepadan dengan output yang dijangkakan.
Atas ialah kandungan terperinci Bagaimanakah saya boleh mengembalikan berbilang data respons dalam satu respons menggunakan PHP dan PDO?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!