What is the difference between bindParam and bindValue?

From the manual entry for PDOStatement::bindParam: [With bindParam] Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called. So, for example: $sex = ‘male’; $s = $dbh->prepare(‘SELECT name FROM students WHERE sex = :sex’); $s->bindParam(‘:sex’, $sex); // use bindParam to bind the variable $sex … Read more

Row count with PDO

$sql = “SELECT count(*) FROM `table` WHERE foo = ?”; $result = $con->prepare($sql); $result->execute([$bar]); $number_of_rows = $result->fetchColumn(); Not the most elegant way to do it, plus it involves an extra query. PDO has PDOStatement::rowCount(), which apparently does not work in MySql. What a pain. From the PDO Doc: For most databases, PDOStatement::rowCount() does not return … Read more

How to properly set up a PDO connection

The goal As I see it, your aim in this case is twofold: create and maintain a single/reusable connection per database make sure that the connection has been set up properly Solution I would recommend to use both anonymous function and factory pattern for dealing with PDO connection. The use of it would looks like … Read more

PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)

As I know, PDO_MYSQLND replaced PDO_MYSQL in PHP 5.3. Confusing part is that name is still PDO_MYSQL. So now ND is default driver for MySQL+PDO. Overall, to execute multiple queries at once you need: PHP 5.3+ mysqlnd Emulated prepared statements. Make sure PDO::ATTR_EMULATE_PREPARES is set to 1 (default). Alternatively you can avoid using prepared statements … Read more