Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO Setting PDO::MYSQL_ATTR_FOUND_ROWS fails

Tags:

php

mysql

pdo

I'm trying to set PDO::MYSQL_ATTR_FOUND_ROWS attribute to true in PDO, but I cannot seem to set it. I am using PHP 5.4.16 and MySQL 5.5.PDO and pdo_mysql both appear in my phpinfo().

Here is how I try to set it to true.

public function __construct () {
    $dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8';
    $this->_db = new PDO($dsn,DB_USER,DB_PASS);

    // The following setAttribute() returns FALSE.
    $this->_db->setAttribute(PDO::MYSQL_ATTR_FOUND_ROWS, TRUE); 
}

I tried to look for every possible settings I can think of. What am I still missing?

like image 503
CookieEater Avatar asked Sep 08 '13 12:09

CookieEater


People also ask

What does PDO mean in PHP?

PDO is an acronym for PHP Data Objects. PDO is a lean, consistent way to access databases. This means developers can write portable code much easier. PDO is not an abstraction layer like PearDB.

What is PDO in MySQL?

PDO (PHP Data Objects) is a general database abstraction layer with support for MySQL among many other databases. It provides prepared statements, and significant flexibility in how data is returned.

How do you write PDO?

$pdo = new PDO($dsn, $user, $passwd); A new PDO object is created. We pass the constructor the data source name and the user name and password. The PDO class represents a connection between PHP and a database server.

How do I install PDO drivers?

Pdo ( Portable Data Object ) needs to be installed if it is not done before. For windows platform go to control panel > Add remove program ( or Programs and features ) > Select your PHP installation and click Change. If you are installing PHP fresh then in the setup wizard you can select PDO from Extensions link.


1 Answers

It seems that PDO::MYSQL_ATTR_FOUND_ROWS is a mysql connection option. Thus, it works only as PDO connection option as well. So, set it up this way

$opt  = array(
    PDO::MYSQL_ATTR_FOUND_ROWS   => TRUE,
    // you may wish to set other options as well
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
);
$this->_db = new PDO($dsn,DB_USER,DB_PASS,$opt);
like image 159
Your Common Sense Avatar answered Sep 21 '22 12:09

Your Common Sense