The $db
which is your class variable PDO has the method prepare, who is responsible for preparing an execution command (SQL) and that the same returns a Pdostatement and in it has the method execute, ie, $db
does not have this method execute
but, it has a method that returns a Pdostatement who has the run.
In the $db
you can directly use the PDO::query, that also return a Pdostatement already executed being the one with data return and PDO::exec, which executes Insert, Update and Delete commands that return the amount of affected lines.
//select
$db->query("SELECT * FROM BD");
//insert, update e delete
$rowsafetados = $db->exec("INSERT INTO BD ...");
'Cause this is all happening?
There is separation of responsibility between classes (POO) and each class has its own responsibility, $db
connection and fast methods, and Pdostatement using the $db
as a connection to your methods.
optionally it can chain calls
$result = $db->prepare("SELECT * FROM BD")->execute();
– HwapX
will even work, but, the problem that ai will not be returned a Pdostatement, it will return a
bool
as described on the PHP.net website (http://www.php.net/manual/en/pdostatement.execute.php), maybe for some cases it works well but it is best to use a Pdostatement– user6026
you’re right, it should be
fetch
orfetchAll
.– HwapX