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.

INSERTquery, yet you're posting the code that revolves around anUPDATE. There's nothing indicating thatexecutegets called twice. Also, the class you created - it's not really doing you any favors, is it? It's more complicated than usingPDOwithout "object oriented" code. How come you are not using ORM libraries available to PHP? If you're dabbling with this, I understand intellectual curiosity, but if you're trying to do some actual work - why reinvent the wheel? There's nothing here that we can use to reproduce, so Charlotte is quite correct.bindValue, notbindParam, in thebind()method, becausebindParambinds to references. You're binding all the parameters to the last parameter.