Reputation: 586
I'm starting to build a little PDO wrapper I will be using for my application. However, when I started coding, I bumped into an issue I can't seem to resolve.
The problem I have is that PDOStatement's execute() is returning false and I don't know if there's something wrong with the value binding or the execution. I've tested the query (which anyway is very simple) and it works fine. Connection to the server is also working fine.
I hope you can help! Here's my code:
<?php
class DataBase {
private $PDO;
private static $instancia;
public static function getInstance() {
if (!self::$instancia instanceof self) {
self::$instancia = new self;
}
return self::$instancia;
}
function __construct() {
$configuracion = Configuracion::getInstance();
// echo "mysql:host={$configuracion->dbHost};dbname=mysql", $configuracion->dbUser, $configuracion->dbPassword;
try {
$this->PDO = new PDO("mysql:host={$configuracion->dbHost};dbname=mysql", $configuracion->dbUser, $configuracion->dbPassword);
debug("conectado a la db", __FILE__, __LINE__);
} catch (PDOException $e) {
debug($e->getMessage(), __FILE__, __LINE__);
}
}
function selectDistanceFromDistances($a, $b) {
$sentencia = $this->PDO->prepare('SELECT distance FROM distances WHERE a = ? AND b = ?;');
// debug($sentencia->execute(array($a, $b)));
$sentencia->bindValue(1, 15, PDO::PARAM_INT);
$sentencia->execute();
$this->PDO->errorInfo();
$resultado = $sentencia->fetchAll();
return $resultado;
}
}
?>
Thanks!
Upvotes: 1
Views: 2750
Reputation: 51411
$sentencia->execute();
$this->PDO->errorInfo();
You're doing the execute, and then asking for error info, but you aren't actually doing anything with the error info! You seem to have a debug function, so that seems like a good idea to use here.
Your query has two placeholders, but you've only bound one of them, so that's probably what the error is.
You might want to consider turning on exceptions mode and using try/catch. PDO is silent by default, outside of the constructor.
Upvotes: 4