i inserting row database. working when enable pdo error message, give me error. want know why error coming can keep error messaging enabled during development. here code:
enabling error put in db class
$this->_pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception);
database connection in class
try { $this->_pdo = new pdo('mysql:host=' . config::get('mysql/host') . ';dbname=' . config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password')); $this->_prefix = ''; } catch (pdoexception $e) { die($e->getmessage()); }
here method call query
public function query($sql, $params = array()) { $this->_error = false; if($this->_query = $this->_pdo->prepare($sql)) { $x=1; if(count($params)){ foreach($params $param){ $this->_query->bindvalue($x, $param); $x++; } } if($this->_query->execute()){ $this->_results = $this->_query->fetchall(pdo::fetch_obj); $this->_count = $this->_query->rowcount(); } else { $this->_error= true; } } return $this; }
this insert function in db class
public function insert($table, $fields = array()){ $keys = array_keys($fields); $values = ''; $x = 1; foreach ($fields $field) { $values .= "?"; if ($x < count($fields)) { $values .= ', '; } $x++; } $sql = "insert {$table} (`" . implode('`,`', $keys) . "`) values ({$values})"; if (!$this->query($sql, $fields)->error()) { return true; } return false; }
this function in tour class saves data db calling insert function.
public function create($fields= array()){ if(!$this->_db->insert("tours", $fields)){ throw new exception('there problem creating tours.' .print_r($this->_db->error())); } }
the reason keep attr_errmode enable because me debug during development , have still many pages develop. have visited many similar questions related error. don't errors when enabled error messaging system tells me in detail.
this different other question not giving me error insert row when enable error mode give me error. using dynamic parameters can't add colon : rectify it. same function used query database.
my humble apologies aynber, right not understand answer have remove fetchall function. have created 2 function 1 insertion/updating , query. answer there seeing other answers.
i removed following line query function
$this->_results = $this->_query->fetchall(pdo::fetch_obj);
thanks class doing proper error handling.
Comments
Post a Comment