Hi.
<?php
define('CONN_STRING', 'mysql:host=;dbname=');
define('DB_USER', '');
define('DB_PASSWORD', '');
function checkQuery($sth){
$err= $sth->errorInfo();
if(count($err)>1){
throw new PDOException($err[2]);
}
}
class RegisteModel{
private $db= null;
public function __construct(PDO $db){
$this->db= $db;
}
public function insert($name,$password,$email,$uid){
$sql= "INSERT INTO users (user_ID, user_name, user_password,user_email, user_confirm, user_is_admin, user_date, user_uid)
VALUES(NULL,:name,MD5password),:email,'0','0',NOW(),:uid)";
$sth= $this->db->prepare($sql);
$excute= array(':name'=>$name,'assword'=>$password,':email'=>$email,':uid'=>$uid);
$sth->execute($excute);
checkQuery($sth);
return $this->db->lastInsertId();
}
}
try{
$db= new PDO(CONN_STRING, DB_USER, DB_PASSWORD);
$registerModel= new RegisteModel($db);
echo $registerModel->insert('91$name','5$password','1$email','1$uid');
$db = null;
}catch(PDOException $e){
echo $e->getMessage();
exit();
}
?>
I think it's quite useless to extends the class RegisteModel
to check the query therefore I made the function
Could anyone tell me what sort of system you can use in this case OOP
speaking ?
Thanks.
ByeYou have, I think, completely missed the point of exceptions. In PDO, you can have it automatically throw an exception if a query fails- this is ALMOST ALWAYS what you want- this means that you don't have to ever write any code explicitly checking the return value of a query EVER AGAIN.
So if you do that right, the checkQuery function is superfluous, and won't be needed at all.
MarkI mean
Do you have to build a wrapper like in 'old' way ie
public function query($query){
if(!(bool)$this->result=$this->mysqli->query($query)){
throw new DbException('Error query : <b>['.$query.']</b> <b style="color:red">Error :['.$this->mysqli->error.']</b> in class <b>['.__CLASS__.']</b>');
}
or it's better to check $registerModel->insert ie with a return false.
ByeYou have, I think, completely missed the point of exceptions. In PDO, you can have it automatically throw an exception if a query fails- this is ALMOST ALWAYS what you want- this means that you don't have to ever write any code explicitly checking the return value of a query EVER AGAIN.
So if you do that right, the checkQuery function is superfluous, and won't be needed at all.
Mark
If the query is totally wrong I agree with you
(see the try catch block)
but
in this case having the username unique the exception
doesn't work I think is better to check the return value.
Thanks for the point.
Bye.Do this:
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
After you connect.
And any query will throw an exception *automatically* with no extra work. This is how PDO is meant to be used.
Don't write any wrappers- it will Just Work.
MarkDo this:
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
After you connect.
And any query will throw an exception *automatically* with no extra work. This is how PDO is meant to be used.
Don't write any wrappers- it will Just Work.
Mark
Thanks a lot buddy I'm happy
Bye.
<?php
define('CONN_STRING', 'mysql:host=;dbname=');
define('DB_USER', '');
define('DB_PASSWORD', '');
function checkQuery($sth){
$err= $sth->errorInfo();
if(count($err)>1){
throw new PDOException($err[2]);
}
}
class RegisteModel{
private $db= null;
public function __construct(PDO $db){
$this->db= $db;
}
public function insert($name,$password,$email,$uid){
$sql= "INSERT INTO users (user_ID, user_name, user_password,user_email, user_confirm, user_is_admin, user_date, user_uid)
VALUES(NULL,:name,MD5password),:email,'0','0',NOW(),:uid)";
$sth= $this->db->prepare($sql);
$excute= array(':name'=>$name,'assword'=>$password,':email'=>$email,':uid'=>$uid);
$sth->execute($excute);
checkQuery($sth);
return $this->db->lastInsertId();
}
}
try{
$db= new PDO(CONN_STRING, DB_USER, DB_PASSWORD);
$registerModel= new RegisteModel($db);
echo $registerModel->insert('91$name','5$password','1$email','1$uid');
$db = null;
}catch(PDOException $e){
echo $e->getMessage();
exit();
}
?>
I think it's quite useless to extends the class RegisteModel
to check the query therefore I made the function
Could anyone tell me what sort of system you can use in this case OOP
speaking ?
Thanks.
ByeYou have, I think, completely missed the point of exceptions. In PDO, you can have it automatically throw an exception if a query fails- this is ALMOST ALWAYS what you want- this means that you don't have to ever write any code explicitly checking the return value of a query EVER AGAIN.
So if you do that right, the checkQuery function is superfluous, and won't be needed at all.
MarkI mean
Do you have to build a wrapper like in 'old' way ie
public function query($query){
if(!(bool)$this->result=$this->mysqli->query($query)){
throw new DbException('Error query : <b>['.$query.']</b> <b style="color:red">Error :['.$this->mysqli->error.']</b> in class <b>['.__CLASS__.']</b>');
}
or it's better to check $registerModel->insert ie with a return false.
ByeYou have, I think, completely missed the point of exceptions. In PDO, you can have it automatically throw an exception if a query fails- this is ALMOST ALWAYS what you want- this means that you don't have to ever write any code explicitly checking the return value of a query EVER AGAIN.
So if you do that right, the checkQuery function is superfluous, and won't be needed at all.
Mark
If the query is totally wrong I agree with you
(see the try catch block)
but
in this case having the username unique the exception
doesn't work I think is better to check the return value.
Thanks for the point.
Bye.Do this:
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
After you connect.
And any query will throw an exception *automatically* with no extra work. This is how PDO is meant to be used.
Don't write any wrappers- it will Just Work.
MarkDo this:
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
After you connect.
And any query will throw an exception *automatically* with no extra work. This is how PDO is meant to be used.
Don't write any wrappers- it will Just Work.
Mark
Thanks a lot buddy I'm happy
Bye.