Introduction
I'm creating a library with some "magic methods" on the PDO , already I did this with MySQLi and it worked beautifully. The intention is to use it in a Framework that I am building , finally it comes from soap.
Problem
Mysteriously my test table is being deleted shortly after (or during, I'm not sure) a INSERT
.
Code
File:teste.php
<?php$con=NewConnectionPDO;echo'<pre>';var_dump($con->getTables());echo'</pre>';$r=$con->insert('tab_teste',Array('id'=>1,'name'=>'FirstRecord','col3'=>'test'))->execute();$log.=($r?'Success':'Fail').PHP_EOL;echo'<pre>';echo$con->lastSQL().PHP_EOL;echo'</pre>';echo'<pre>';var_dump($con->getTables());echo'</pre>';?>
Class:ConnectionPDO
<?phpclassConnectionPDOextendsPDO{function__construct($dsn,$username=NULL,$password=NULL,$options=NULL){parent::__construct($dsn,$username,$password,$options);$this->LoadDriverMethods();}privatefunctionLoadDriverMethods(){$driver=__DIR__.DIRECTORY_SEPARATOR.'drivers'.DIRECTORY_SEPARATOR.'sqldriver.'.strtolower($this->getAttribute(PDO::ATTR_DRIVER_NAME)).'.php';if(!is_file($driver))thrownewException('Nãofoipossívelcarregarosmétodosdodriver',1);require_once$driver;$this->driver=newSQLDriver();}publicfunctioninsert($table,$data){$this->lastSQL=$this->driver->insert($table,$data);$stmt=$this->prepare($this->lastSQL);$this->driver->setParams($this->stmt);$this->log.=$this->driver->flushLog();return$this->stmt;}}?>
Class:SQLDriver
<?phpclassSQLDriverimplementsDriverInterface{publicfunctioninsert($table,$data){$this->clearParams();$sql="INSERT INTO '{$table}' ";
$colunms = Array();
$values = Array();
foreach ($data as $col => $value) {
$colunms[] = "'{$col}'";
$values[] = '?';
$this->addParam($col, $value);
}
$sql .= '(' . implode(', ', $colunms) . ') VALUES (' . implode(', ', $values) . ');';
$this->log .= $sql . PHP_EOL;
return $sql;
}
public function addParam($key, $value){
//$this->params[':'.$key] = $value;
$this->params[] = $value;
}
public function setParams(PDOStatement $stmt){
$params = $this->getParams();
$this->log .= 'Setando Parâmetros: '.PHP_EOL;
if (is_array($params) && !empty($params)){
foreach ($params as $param => $value){
$stmt->bindValue($param+1, $this->prepareParam($value), $this->getParamType($value));
$this->log .= $param+1 . ' => ' . $this->prepareParam($value) . PHP_EOL;
}
}
$this->log .= PHP_EOL.'-----------------------------'.PHP_EOL.PHP_EOL;
}
}
?>
I just put the code around the error. The complete code can be seen in the links:
Unless someone downloads the code and keeps testing and searching I do not think you'll find the problem so easily, so the main question here is, How to debug the PDO? The secondary is > what could be causing this? The third, why?