合并PHP中几个表的结果

时间:2014-12-14 14:32:52

标签: php json mysqli array-merge

我试图按标题搜索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();
?>

0 个答案:

没有答案