Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use PDO in CodeIgniter 2?

Tags:

I would like to use PDO instead of the built-in database-classes. But how can I do it? I tried to do the modifications suggested in a blog post. I commented out a part of system\database\DB.php and also added:

$DB = new PDO( $params['dbdriver'].':host='.$params['hostname'].';dbname='.$params['database'], $params['username'], $params['password']);  return $DB; 

But now my PHP-code crashes if I do $this->load->database(); in a Controller. So how should I use PDO and execute database queries using PDO in CodeIgniter?

I have also tried to use this code in a Controller:

foreach($this->db->query('SELECT * FROM users') as $row) {     print_r($row); } 

but it didn't work.

like image 204
Jonas Avatar asked May 04 '11 14:05

Jonas


People also ask

Does CodeIgniter use PDO?

I'm pretty sure CodeIgniter uses PDO under the hood. EDIT: The MySQLi driver uses mysqli_* . They have a PDO Driver you can use.

How does PHP PDO work?

PDO—PHP Data Objects—are a database access layer providing a uniform method of access to multiple databases. It doesn't account for database-specific syntax, but can allow for the process of switching databases and platforms to be fairly painless, simply by switching the connection string in many instances.

How does PHP PDO fetch data?

Fetch data from a result set by calling one of the following fetch methods: To return a single row from a result set as an array or object, call the PDOStatement::fetch method. To return all of the rows from the result set as an array of arrays or objects, call the PDOStatement::fetchAll method.

What is PDO extension in PHP?

The PHP Data Objects ( PDO ) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions.


1 Answers

$stmt = $this->db->prepare("SELECT * FROM users");   $stmt->execute();   foreach ($stmt->fetch(PDO::FETCH_ASSOC) as $row) {       print_r($row); } 

Or:

foreach ($this->db->query("SELECT * FROM users") as $row) {        print_r($row)."\n"; }     

Learn more about 3 PDO database calls here...

PDO Query
PDO Exec
PDO Prepare

EDIT: Also check your app/config/database.php file for the following settings:

$active_group = 'default';   $active_record = FALSE;    $db['default']['hostname'] = 'YOURHOSTNAME';   $db['default']['username'] = 'YOURUSERNAME';   $db['default']['password'] = 'YOURPASSWORD';   $db['default']['database'] = 'YOURDATABASE';   $db['default']['dbdriver'] = 'mysql';   $db['default']['dbprefix'] = '';   $db['default']['pconnect'] = TRUE;   $db['default']['db_debug'] = TRUE;   $db['default']['cache_on'] = FALSE;   $db['default']['cachedir'] = '';   $db['default']['char_set'] = 'utf8';   $db['default']['dbcollat'] = 'utf8_general_ci';   $db['default']['swap_pre'] = '';   $db['default']['autoinit'] = TRUE;   $db['default']['stricton'] = FALSE;  
like image 188
csi Avatar answered Oct 15 '22 23:10

csi