|
|||||
PHP+PDO+MySQL: How I’m doing it and a question…I recently decided to rewrite an application that I created for an English school where I used to work, and this time I want to do it right. I chose to write it in PHP with Object Orientation in mind, and use PDO+MySQL for the DAO portion. The reason for using PDO is that it's the closest thing to the DBI that I found for PHP - and the reason for PHP is that it's just plain simpler than using pure Perl, and I don't have the time to learn Catalyst or some other Perl Web framework. Anyway, here's what I'm doing for the PDO portion. First I created a
<?php
class Db{
public $dbh = null;
public function Db($db_type,$db_host,$db_user,
$db_pass,$db_base,$tbl_prefix){
if ($db_type == 'mysql') { // in case I want to add oracle in the future
$mysql_DSN = "mysql:host=".$db_host.";dbname=".$this->db_base;
try {
$this->dbh = new PDO($mysql_DSN, $db_user, $db_pass,
array(PDO::ATTR_PERSISTENT => true, PDO::ERRMODE_WARNING => true,
PDO::ATTR_ERRMODE => true));
}
catch(PDOException $e){
$rc[msg] = $e->getMessage();
die($rc[msg] . " " . __LINE__);
}
return $this->dbh;
}
}
}
?>
Secondly, I created the DAO classes which extend the DB. They are responsible for
<?php
class StudentsDao extends Db {
public $sth = array();
// Constructor
public function StudentsDao($db_type=NULL,$db_host=NULL,$db_user=NULL,
$db_pass=NULL,$db_base=NULL,$tbl_prefix=NULL) { // These params are to be passed to Db class
// Connect to DB
$this->Db($db_type,$db_host,$db_user,
$db_pass,$db_base,$tbl_prefix);
// Prepares queries
$this->sth = $this->prepare($tbl_prefix); // Look at prepare() further down
} // end of function StudentsDao
// Function that prepares queries
public function prepare() {
$sth = null; // will hold our temporary array
// get all students
$string = sprintf('select * from %s.%sstudents where id = ?',DB_BASE,TBL_PREFIX); // I have these constants defined in the main page
$sth['getStudentById'] = $this->dbh->prepare($string);
// update Student by Id
$string = sprintf('update %s.%sstudents
set field1 = ?, field2 = ?, field3 = ?
where id = ?',DB_BASE,TBL_PREFIX); // Note that this table has 3 control fields (id, sys_creation_date, and sys_update_date) prior to field1, field2, and field3 - I don't want them to be touched
$sth['updStudentById'] = $this->dbh->prepare($string);
return $sth;
} // end of prepare()
// Here we have the handlers for the queries prepared above.
// Fetches all students
public function getStudentById($id) {
$s = $this->sth['getStudents'];
$s->execute(array($id)) || die(print_r($s->errorInfo,1));
$result = $s->fetchAll(PDO::FETCH_ASSOC);
if (sizeof($result) == 0) {
// no data found
$out[err_cd] = 1;
}
else {
// data was found
$out[err_cd] = 0;
$out[data] = $result;
}
return $out;
}
// Updates student by Id - this is a tricky one
public function updateStudentById($data,$id){ // $data can have data regarding one or more fields
// Populate current fields
$fields = $this->getStudentById($id); // get current values
// Remove unwanted fields
$fields = array_slice($fields[data][0],3); // remove the 3 initial control fields
// Replace with new fields
foreach($data as $k=>$v){
$fields[strtolower($k)] = $v;
}
$fields['id'] = $id;
$s = $this->sth['updStudentById'];
$s->execute(array_values($fields)) || die(print_r($s->errorInfo,1));
$count = $s->rowCount();
if ($count == 1) {
$out[err_cd] = 0;
}
else {
$out[err_cd] = -1;
}
$out[err_msg] = $this->status[$out[err_cd]];
return $out;
}
?>
In the example above, you will probably frown on the fact that I'm calling I wanted to be able to pass the function an associative array containing only the fields I want to update - be it only My original idea was to basically, to emulate something like this:
That would set Here's the question part: The technique shown in the example will basically pull all values from the record and populate them in the 1 comment to PHP+PDO+MySQL: How I’m doing it and a question… |
|||||
|
Copyright © 2010 use strict ;#) - All Rights Reserved |
|||||
thanks for the nice info you provided in your post. Although your post seems preety old, but it still helped me
Thanks again