dtzjvj3915 2015-11-06 22:51
浏览 48
已采纳

关闭连接以使剩余查询运行[关闭]

Perhaps I'm missing something very obvious here.

My code doesn't seem to work below the 'GET BOOK ID' annotation if I don't close and reopen the connection.

While it does work, it doesn't seem to be an efficient way at all to do this, any suggestions?

$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条回答 默认 最新

  • duanqian2278 2015-11-06 23:20
    关注

    You don't have to keep including the connect.php file over and over. Assuming you already included it somewhere at the top since you run $AddBook->execute()

    See this updated code:

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退