2017-04-24 60 views
0

我对PHP很新颖,当前的问题是必须在同一页面上运行sql查询。多个sql查询运行问题

我在同一页上有两个不同的div。一个div显示商店程序中的所有笑话,另一个div显示来自简单选择的作者。

我从这个错误信息能得到什么:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.' in /usr/local/pem/vhosts/272348/webspace/httpdocs/icloudis.com/sohail/crud_cssgrids/aeroplane.php:18 Stack trace: #0 /usr/local/pem/vhosts/272348/webspace/httpdocs/icloudis.com/sohail/crud_cssgrids/aeroplane.php(18): PDO->query('select * from p...') #1 {main} thrown in /usr/local/pem/vhosts/272348/webspace/httpdocs/icloudis.com/sohail/crud_cssgrids/aeroplane.php on line 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> 

-thanks

回答

-1

取所有返回所有行作为一个大数组。 U可以在运行$行中的下一个查询之前保存结果。

include_once("config.php"); 

$result = $pdo->query("call aeroplane"); 
$rows = $result->fetchAll(); 
$result->closeCursor(); 

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

//the $result query 
foreach($rows as $row) {   
     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>"; 


} 
1

您发布的代码不是真实的代码。这使得代码和错误彼此无关,而这又使得它不可能回答。

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

无论如何,为了使您的代码正常工作,您应该在关闭游标之前取回您的数据,然后。因为关闭后明显会有将无数据取回。

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

该错误与缓冲查询无关,但与stored procedures' behavior