繁体   English   中英

多个SQL查询运行问题

[英]multiple sql queries running issue

我对PHP相当陌生,当前的问题是必须在同一页面上运行sql查询。

我在同一页面上有两个不同的div。 一个div显示存储过程中的所有笑话,而另一个div则显示作者的简单选择。

我从此错误消息中得到了什么:

致命错误:消息为'SQLSTATE [HY000]的未捕获异常'PDOException':常规错误:2014当其他未缓冲的查询处于活动状态时,无法执行查询。 考虑使用PDOStatement :: fetchAll()。 另外,如果您的代码只打算针对mysql运行,则可以通过设置PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY属性来启用查询缓冲。 在/usr/local/pem/vhosts/272348/webspace/httpdocs/icloudis.com/sohail/crud_cssgrids/aeroplane.php中:18堆栈跟踪:#0 / usr / local / pem / vhosts / 272348 / webspace / httpdocs / icloudis .com / sohail / crud_cssgrids / aeroplane.php(18):PDO-> query('select * from p ...')#1 {main}放在/ usr / local / pem / vhosts / 272348 / webspace / httpdocs中/icloudis.com/sohail/crud_cssgrids/aeroplane.php,第18行

我从该消息中了解到,我不能同时有几个无缓冲的查询? 所以,我需要使用fetchAll或

setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);

并使用closeCursor()。 我不明白的是如何利用所有这些使它起作用。

这是我的配置文件:

try {


    $pdo = new PDO('mysql:host=xx.xxx.xxx.xx; dbname=xxxxxxx', 'xxxx', 'xxxxxx');
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
    $pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
} catch (PDOException $e) {

    echo 'Connection failed: ' . $e->getMessage();

    die();

}

我想要查询的页面,我没有尝试在这里放入第二个查询的结果,但是当前代码是:

 <?php
//including the database connection file
//include_once("config_local.php");
include_once("config.php");

session_start();
if(empty($_SESSION['email']))
{
 header("location:index.php");
}

echo "Welcome ".$_SESSION['name']; 



$result = $pdo->query("call aeroplane");
$result->closeCursor();
$result1 = $pdo->query("select * from plane_maker");
$result1->closeCursor();

?>
<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title>Aeroplanes</title>
        <link rel="stylesheet" href="css/styles.css">
    </head>
    <body>
<section class="grid-1">
  <div class="item-1">1</div>

  <div class="item-2">
    <div class="item-5">
    <a href="add_form.php">Add New Aeroplane</a> &nbsp; | &nbsp;
        <a href="aeroplane_maker.php">Add New aeroplane maker</a>
        <br/><br/>

    <table width='80%' border=0>

    <tr bgcolor='#CCCCCC'>
        <td>Maker Name</td>
        <td>Aeroplane</td>
        <td>Top speed</td>
        <td>Range</td>
        <td>Update</td>
    </tr>
    <?php

    while($row = $result->fetchAll()) {         
        echo "<tr>";
        echo "<td>".$row['planeMakerName']."</td>";
        echo "<td>".$row['aeroplaneName']."</td>";
        echo "<td>".$row['aeroplaneTopSpeed']."</td>";
        echo "<td>".$row['aeroplaneRange']."</td>";
        echo "<td><a href=\"edit.php?id=$row[aeroplaneID]\">Edit</a> | <a href=\"delete.php?id=$row[aeroplaneID]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";        
    }

    ?>
    <a href="logout.php">Logout</a>
    </table>
    </div>
    <div class="item-6">6</div>
    <div class="item-7">7</div>
  </div>

  <div class="item-3">3</div>
  <div class="item-4">4</div>
</section>
    </body>
</html>

-谢谢

您发布的代码不是真实的代码。 这使得代码和错误彼此不相关,从而使得无法回答。

您应该始终发布导致错误的确切代码。

无论如何,要使代码正常工作,您应该关闭游标之前获取数据。 因为在关闭之后, 显然将没有任何数据可获取。

$result = $pdo->query("call aeroplane")->fetchAll();
$result->closeCursor();
$result1 = $pdo->query("select * from plane_maker")->fetchAll();

该错误与缓冲查询无关,而与存储过程的行为有关

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM