Ch: 002 PDO - heikijapan/php-book2 GitHub Wiki

PDO is safe way to connect to MySQL databases using PHP.

Building pagination

It is good to know that SQL's keywords LIMIT and OFFSET always need INT otherwise script will show an error.

When setting up pdo then it is recommended to have these settings

$pdo_settings =
[
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES => false,
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
  • this will hand over SQL script to directly to MySQL without emulating values. Therefore MySQL can process all values as needed. PDO::ATTR_EMULATE_PREPARES => false is the setting responsible for this. When this setting is set to FALSE and using :placeholder in execute() will work. But there is no 100% guarantee of it working. Therefore when building PAGINATION and using LIMIT and OFFSET then bindValues() will be more safer.
$stmt->bindValues(':perPage', $perPage, PDO::PARAM_INT);
  • PDO::PARAM_INT will make sure that values will be INT