2
The following code returns 11 records from the database:
$dbh = new PDO('sqlite:db_coleta.sqlite3');
$sth = $dbh->prepare('SELECT * FROM ROTA_VIEW WHERE usuario_id = 1 AND 0 = 0');
$sth->execute();
$red = $sth->fetchAll();
var_dump($red);
But the following code, using parameter, does not return any record (which is wrong):
$dbh = new PDO('sqlite:db_coleta.sqlite3');
$sth = $dbh->prepare('SELECT * FROM ROTA_VIEW WHERE usuario_id = ? AND 0 = 0');
$sth->execute(array(1));
$red = $sth->fetchAll();
var_dump($red);
I would like to know what I am doing wrong, because I am following examples of php documentation, all the same, only for my problem. I cannot understand why this does not work, since it is the same thing as the following example (taken from http://php.net/manual/en/pdo.prepare.php):
/* Execute a prepared statement by passing an array of values */
$sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < ? AND colour = ?');
$sth->execute(array(150, 'red'));
$red = $sth->fetchAll();
$sth->execute(array(175, 'yellow'));
$yellow = $sth->fetchAll();
Okay, it worked, thanks.
– Evandro Weiss
Always have a reference, documentation with you, so you don’t get lost in the basic things. And remember, bindValue() does not guarantee data security, it serves to be able to reuse the query.
– Matheus Picioli
Yes, I always check, but the following phrase escaped from the manual: "All values are treated as PDO::PARAM_STR."(http://php.net/manual/en/pdostatement.execute.php). Anyway, I had other querys running before, but only with string type parameters, so the way I had done it worked. Finally, I changed to use bindParam, as suggested.
– Evandro Weiss