PDO Multi-query “SQLSTATE[HY000]: General error”

前端 未结 2 896
野趣味
野趣味 2021-01-07 09:23

I\'m still learning PDO so I might of missed something but basically I\'m trying to insert a row into a table and then select the generated id.

I\'m not sure if it l

相关标签:
2条回答
  • 2021-01-07 09:39

    I'm pretty sure the mysql driver for PDO (maybe mysql itself?) does not support multi-query prepared statements.

    Instead of SELECT LAST_INSERT_ID() in your query, use Conexion::$cn->lastInsertId() after your $query->execute()

    0 讨论(0)
  • 2021-01-07 09:40

    I think this is correct:

    function ExecuteQuery($sql, $params = array())
    {
        if(Conexion::$cn== null)
            Conexion::Connect();
        $paramString="";
    
        foreach($params as $k=>$v)
        {
            $param = " :".$k." ,";
            $paramString .= $param;
        }
        $sql.=substr($paramString,0,-2);
        $query = Conexion::$cn->prepare($sql);
        foreach($params as $key => $value)
        {
            echo "entro";
            $query->bindParam(":".$key, $value);
        }
    
        $query->execute();
        $x = $query->fetchAll(\PDO::FETCH_ASSOC);
        var_dump($x);
        return $x;
    }
    
    public function Connect()
    {
        try {
            $dns='dblib:host='.Conexion::$server.";dbname=".Conexion::$db.";";
            Conexion::$cn = new \PDO($dns, Conexion::$user, Conexion::$passw);
            Conexion::$cn->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
        }
        catch(PDOException $e) 
        {
            echo $e->getMessage();
        }
    }
    
    0 讨论(0)
提交回复
热议问题