douyan1613 2017-04-07 13:33
浏览 63
已采纳

php postgresql错误,准备好的语句已经存在

Am getting an error of prepared statement "my_query7" already exists, i call this function each time a user tries to update table leader_info in the database, i have gone through the documentation for pg_prepare and i don't understand what is meant by it should only be run once. code snippets will be of help. Thanks.

 function add_leader_country($user_id,$l_country)
 {
   global $connection;
   $query = pg_prepare($connection,"my_query7","update leader_info set l_country = $1 where user_id = $2 and status < 9"); 
$result = pg_execute($connection,"my_query7",array($l_country,$user_id));
if(!$result)
{
  echo pg_last_error($connection);
} 
else 
{
  echo "Records created successfully
";
}
$row = pg_affected_rows($result);
return $row;



 }
  • 写回答

1条回答 默认 最新

  • douqiaotong8682 2017-04-07 13:53
    关注

    Prepare execute does not permit duplicate naming, so that is your error. A query should only be prepared once, for example, in a cycle for the preparation state must be set out of the for and its execution in the for.

     $result=$pg_prepare($connection,"my_query7",$query);
        for($id=1;$id<3;$id++){
          $result=pg_execute($connection,"my_query7",array($l_country,$user_id));
           ...
        }
    

    In your case using a functio that use the prepare and execute multiple times it's a problem.

    What are you trying to accomplish with this function dispatches more code like where you are calling the function. This way I might be able to help you.

    If you want to use functions I would use this method

    Exemple from https://secure.php.net

    <?php
    function requestToDB($connection,$request){
        if(!$result=pg_query($connection,$request)){
            return False;
        }
        $combined=array();
        while ($row = pg_fetch_assoc($result)) {
            $combined[]=$row;
        }
        return $combined;
    }
    ?>
    
    <?php
    $conn = pg_pconnect("dbname=mydatabase");
    
    $results=requestToDB($connect,"select * from mytable");
    
    //You can now access a "cell" of your table like this:
    $rownumber=0;
    $columname="mycolumn";
    
    $mycell=$results[$rownumber][$columname];
    var_dump($mycell); 
    

    If you whant to use preaper and execute functions try to create a function that creates the preparations only once in a session. Do not forget to give different names so that the same error does not occur. I tried to find something of the genre and did not find. If you find a form presented here for others to learn. If in the meantime I find a way I present it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看