I have looked everywhere for an answer to this and tried everything I have found here on StackOverflow and other sites.
Basically what's happening is that whenever I execute an SQL query, PDO is executing it twice even when execute()
is only called once...
Here is my code...
<?php
namespace quizazle;
class sql{
private $username = 'x';
private $passwd = '';
private $port = 3306;
private $host = 'x';
private $name = 'x';
private $charSet = 'utf8mb4';
private $db = null;
public function __construct(){
$this->db = new \PDO("mysql:host=$this->host;dbname=$this- >name;charset=$this->charSet", $this->username, $this->passwd);
$this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
$this->db->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
}
private function bind($q, $p){
try{
$s = $this->db->prepare($q);
if(!empty($p)){
foreach ($p as $pm){
$s->bindParam($pm['key'], $pm['value']);
}
}
}
catch(PDOException $e){
echo($e->getMessage());
}
return $s;
}
public function query($query, $params){
try{
$statement = $this->bind($query, $params);
$statement->execute();
$res = $statement->fetchAll(\PDO::FETCH_BOTH);
return $res;
}
catch(PDOException $e){
echo($e->getMessage());
}
}
public function update($query, $params){
try{
$statement = $this->bind($query, $params);
$statement->execute();
$res = $statement->rowCount();
return $res;
}
catch(PDOException $e){
echo($e->getMessage());
}
}
}
?>
and here is the code where I am using the sql
class...
$sql = new quizazle\sql();
$params = array(
array(
'key' => 'd',
'value' => 'a'
)
);
$result = $sql->update("INSERT INTO `test` (`data`) VALUES(:d)", $params);
var_dump($result);
I have tried...
- removing
var_dump($result);
- not binding parameters, although that is a bad way to go for me, it still doesn't work.
- changing
$this->db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
to$this->db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
- changing
$this->db->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
to$this->db->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
yet none of this has worked. Any help will be greatly appreciated and thank you all in advance :)
PS: I discovered this problem while running INSERT statements, if that helps at all.