json — объединение результатов нескольких таблиц в переполнении стека

Я пытаюсь найти 5 таблиц по названию и вернуть результаты в массив, а затем объединить 5 массивов, чтобы они могли быть закодированы в JSON и отображены с помощью mysqli. Если результат из первой таблицы, из таблицы книг, он выводится нормально, но если результат из других 4 таблиц, он выводит [[]]. Любая помощь или советы будут оценены. Благодарю.

Вот мой код:

<?php
$mysqli = new mysqli("localhost", "root", "", "mediainventory");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ")" . $mysqli->connect_error;
}
if (!($stmt = $mysqli->prepare("SELECT Library_ID, Title, Author, Type, Department, Status, Due_Date FROM Book WHERE Title=?"))){
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->bind_param("s", $_GET['title'])) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
$id = NULL;
$title = NULL;
$creator = NULL;
$type  = NULL;
$department = NULL;
$status = NULL;
$dueDate = NULL;
$book = array();
$music = array();
$movie = array();
$software = array();
$periodical = array();if (!$stmt->bind_result($id, $title, $creator, $type, $department, $status, $dueDate)) {
echo "Binding result failed: (" . $stmt->errno . ") " . $stmt->error;
}

while ($stmt->fetch()) {
$book = array($id, $title, $creator, $type, $department, $status, $dueDate);
}
if (!($stmt = $mysqli->prepare("SELECT Library_ID, Title, Artist, Type, Department, Status, Due_Date FROM Music WHERE Title=?"))){
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->bind_param("s", $_GET['Title'])) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
if(!$stmt->bind_result($id, $title, $creator, $type, $department, $status, $dueDate)) {
echo "Binding result failed: (" . $stmt->errno . ") " . $stmt->error;
}
while ($stmt->fetch()) {
$music = array($id, $title, $creator, $type, $department, $status, $dueDate);
}
if (!($stmt = $mysqli->prepare("SELECT Library_ID, Title, Director, Type, Department, Status, Due_Date FROM Movie WHERE Title=?"))){
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->bind_param("s", $_GET['Title'])) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->bind_result($id, $title, $creator, $type, $department, $status, $dueDate)) {
echo "Binding result failed: (" . $stmt->errno . ") " . $stmt->error;
}
while ($stmt->fetch()) {
$movie = array($id, $title, $creator, $type, $department, $status, $dueDate);
}
if (!($stmt = $mysqli->prepare("SELECT Library_ID, Title, Developer, Type, Department, Status, Due_Date FROM Software WHERE Title=?"))){
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->bind_param("s", $_GET['Title'])) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->bind_result($id, $title, $creator, $type, $department, $status, $dueDate)) {
echo "Binding result failed: (" . $stmt->errno . ") " . $stmt->error;
}
while ($stmt->fetch()) {
$software = array($id, $title, $creator, $type, $department, $status, $dueDate);
}
if (!($stmt = $mysqli->prepare("SELECT Library_ID, Title, Publisher, Type, Department, Status, Due_Date FROM Periodical WHERE Title=?"))){
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->bind_param("s", $_GET['Title'])) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->bind_result($id, $title, $creator, $type, $department, $status, $dueDate)) {
echo "Binding result failed: (" . $stmt->errno . ") " . $stmt->error;
}
while ($stmt->fetch()) {
$periodical = array($id, $title, $creator, $type, $department, $status, $dueDate);
}
$output[] = array_merge($book, $movie, $music, $software, $periodical);
if (empty($output)){
echo "false";
}
else {
echo json_encode($output);
}
$stmt->close();
$mysqli->close();
?>

1

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]