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

报告相同问题?

悬赏问题

  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路