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 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么
  • ¥15 Linux下安装搜狗拼音不能显示中文
  • ¥15 事件7000导致的自动重启如何解决
  • ¥15 (标签-matlab|关键词-m3)