关闭连接以便运行剩余的查询

时间:2015-11-06 22:51:55

标签: php mysql mysqli

也许我错过了一些非常明显的东西。

我的代码似乎无法在' GET BOOK ID'如果我不关闭并重新打开连接,则注释。

虽然它确实有效,但它似乎并不是一种有效的方式来做这个,有什么建议吗?

$AddBook = $conn->prepare("CALL makeBooking((SELECT CustID FROM customer WHERE UserName = '$UserName'), ?, ?, ?, ?, now())");
$AddBook->bind_param('iiis',$PerfID, $NumAdults, $NumChilds, $ColTicket);
if ($AddBook->execute())
{
    //----------------DEDUCT SEATS--------------//
    $SeatDeduction = $conn->prepare("CALL deductSeats($TotalSeats,?)");
    $SeatDeduction->bind_param('i',$PerfID);
    $SeatDeduction->execute();
    mysqli_close($conn);
    require ('connect.php');
    //-----------------GET BOOK ID--------------//
    $getBookID = "CALL getBookByUserName('$UserName')";
    $result2 = mysqli_query($conn, $getBookID);
    $Output2 = mysqli_fetch_assoc($result2);
    $BookID = $Output2['BookID'];
    mysqli_close($conn);
    require ('connect.php'); 
    //-------------------SET COST---------------//
    $setCost = "CALL setBookingPrice($BookID)";
    mysqli_query($conn,$setCost);
    mysqli_close($conn);
    require ('connect.php');
    //-------------------GET COST---------------//
    $getCost = "CALL getCost($BookID)";
    $result6 = mysqli_query($conn,$getCost);
    $Output6 = mysqli_fetch_assoc($result6);
    $Cost = $Output6['TotalCost'];
    mysqli_close($conn);
    require ('connect.php');
    //---------------BOOKING CONFIRM------------//
    $ShowRef = 'Booking Completed<p>Reference Number: <b>' . $BookID . '</b><p>';
    $showCost = 'Total Cost: <b><u>£' . $Cost . '<u><b><br>';
    $Confirm = $ShowRef.$showCost;
}
else
{
    die(mysqli_error($conn));
}
mysqli_close($conn);

1 个答案:

答案 0 :(得分:0)

您不必一遍又一遍地包含connect.php文件。 假设您运行$AddBook->execute()

后已将其包含在顶部的某个位置

请参阅此更新的代码:

$AddBook = $conn->prepare("CALL makeBooking((SELECT CustID FROM customer WHERE UserName = '$UserName'), ?, ?, ?, ?, now())");
$AddBook->bind_param('iiis',$PerfID, $NumAdults, $NumChilds, $ColTicket);
if ($AddBook->execute())
{
    //----------------DEDUCT SEATS--------------//
    $SeatDeduction = $conn->prepare("CALL deductSeats($TotalSeats,?)");
    $SeatDeduction->bind_param('i',$PerfID);
    $SeatDeduction->execute();

    $SeatDeduction->close();
    $conn->next_result();

    // mysqli_close($conn); // remove this
    // require ('connect.php'); // remove this
    //-----------------GET BOOK ID--------------//
    $getBookID = "CALL getBookByUserName('$UserName')";
    $result2 = mysqli_query($conn, $getBookID);
    $Output2 = mysqli_fetch_assoc($result2);
    $BookID = $Output2['BookID'];
    // mysqli_close($conn); remove this
    // require ('connect.php');  remove this
    //-------------------SET COST---------------//
    // ...
    //-------------------GET COST---------------//
    // ...
    //---------------BOOKING CONFIRM------------//
    // ...
}
else
{
    die(mysqli_error($conn));
}

mysqli_close($conn);