3
Introducing
I’m creating a library with some "magic methods" on top of PDO, already I did it with Mysqli and worked beautifully. The intention is to use in a Framework I am building, finally no more jabá.
Problem
Mysteriously my test table is being deleted right after (or during, I’m not sure) INSERT
.
Code
Filing cabinet: php test.
<?php
$con = New ConnectionPDO;
echo '<pre>';
var_dump( $con->getTables() );
echo '</pre>';
$r = $con->insert('tab_teste',Array('id'=>1,'name' => 'First Record', '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
<?php
class ConnectionPDO extends PDO {
function __construct($dsn, $username = NULL, $password = NULL, $options = NULL) {
parent::__construct($dsn, $username, $password, $options);
$this->LoadDriverMethods();
}
private function LoadDriverMethods(){
$driver = __DIR__ . DIRECTORY_SEPARATOR . 'drivers' .
DIRECTORY_SEPARATOR . 'sqldriver.' .
strtolower($this->getAttribute(PDO::ATTR_DRIVER_NAME)) . '.php';
if (!is_file($driver))
throw new Exception('Não foi possível carregar os métodos do driver', 1);
require_once $driver;
$this->driver = new SQLDriver();
}
public function insert($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
<?php
class SQLDriver implements DriverInterface {
public function insert($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 don’t think you’ll find the problem so easily, then the main question here is, how to debug the PDO? High school is, what might be causing this? The third, why?
Perhaps when you have more time, enter the
script
. So far, good luck.– Edilson
How to inspect the script?
– KaduAmaral
See what’s wrong with it -, you said you have an error/problem, which can be located only with tests.
– Edilson
Does any error appear for you? I am testing and here the times of failure in the query.
– rray
No error appears, just reports as if everything is fine. For example, notice that you have a log variable, where you store "Success" or "fail" from the first INSERT, for me it appears every time Success and then the DELETE and UPDATE scripts work normally too. However, from the first INSERT the table has already disappeared from the database, so I do not understand, if this error would be more justifiable.
– KaduAmaral
So the only thing I noticed wrong was in Sert, from Pdoconnection, a temporal varietal is created
$stmt
and you move on$this->stmt
. https://github.com/KaduAmaral/ConnectionPDO/blob/master/ConnectionPDO.php#L52 , oselect()
tbm has this.– rray
Caraca, now explains everything, since the statement stored in the variable
$this->stmt
is that of the previously executed method$con->drop()
... :The I spent all day and I didn’t notice it. I’m home now, there’s no way to test it, but if you move there to$this->stmt
instead of$stmt
works the$con->select
down below?– KaduAmaral
I tested this way http://pastebin.com/w5PRTcHM with the modifications I suggested in the previous comment.
– rray
Beauty I think is the problem, tomorrow I test. I have to finish a college job here. Thanks, if you want to post as answer tomorrow when I test I mark.
– KaduAmaral
Output from the code is that one
– rray
- That’s interesting, I’ve already downloaded the code with the hope of being able to read today. But if this is the problem, it shouldn’t cost anything to solve.
– Edilson