PDO apparently has no means to count the number of rows returned from a select query (mysqli
has the num_rows
variable).
Is there a way to do this, short of using count($results->fetchAll())
?
PDO apparently has no means to count the number of rows returned from a select query (mysqli
has the num_rows
variable).
Is there a way to do this, short of using count($results->fetchAll())
?
PDO does not escape the variables. The variables and the SQL command are transferred independently over the MySQL connection. And the SQL tokenizer (parser) never looks at the values. Values are just copied verbatim into the database storage without the possibility of ever causing any harm. That's why there is no need to marshall the data with prepared statements.
Note that this is mostly a speed advantage. With mysql_real_escape_string() you first marshall your variables in PHP, then send an inefficient SQL command to the server, which has to costly segregate the actual SQL command from the values again. That's why it's often said that the security advantage is only implicit, not the primary reason for using PDO.
If you concat the SQL command and don't actually use prepared statments (not good!), then yes, there still is an escape function for PDO: $pdo->quote($string)
The following works for me:
<?php
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "pass");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$stmt = $pdo->prepare("INSERT INTO `null_test` (`can_be_null`) VALUES (:null)");
$stmt->bindValue(":null", null, PDO::PARAM_NULL);
$stmt->execute();
Pass in PHP's null
, with type of PDO::PARAM_NULL
. Also, make sure your prepare emulation is set to false. That might help.
lastInsertId() is a method of the PDO class, not the PDOStatement class.
This should work:
$groupID = $dbo->lastInsertId();
Well, at second glance your question looks more complex to be answered with just one link
How does php pdo's prepared statements prevent sql injection?
How can prepared statements protect from SQL injection attacks?
What are other pros/cons of using PDO?
Most interesting question.
A greatest PDO disadvantage is: it is peddled and propagated a silver bullet, another idol to worship.
While without understanding it will do no good at all, like any other tool.
PDO has some key features like
Does using PDO reduce efficiency?
Again, it is not PDO, but prepared statements that reduces efficiency. It depends on the network latency between the db server and your application but you may count it negligible for the most real world cases.
According to the manual, there is a
PDOStatement->rowCount
method ; but it shouldn't be used (quoting) :If you already have a recordset, and want to know how many lines are in it, you'll have to fetch the data, using one of the
fetch*
methods ; and use count -- like you suggested.