php - Using database class with PDO -
the classic first liner - have seen similar questions on none me understand crossroads at.
a little background before code - not new php or pdo (though not expert either), complete newbie object oriented php , trying balance right of when use classes , when possibly overkill.
the answer i'm hoping in 2 parts. firstly, practice create wrapper database class when using pdo - connection, basic queries etc.
secondly, if not - there better ways speed query writing?
edit whilst questioning code below, question pdo wrapper class approach overall - class below larger this, there need/benefit?
see following code; note: class file called via spl_autoload_register() in config.php
class_database.php
class database { private $conn; public function __construct() { $this->openconnection(); } public function openconnection() { try { $this->conn = new pdo('mysql:host=' . db_server . '; dbname=' . db_name, db_user, db_pass); $this->conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); } catch (pdoexception $e) { echo 'there error connecting database, error: ' . $e->getmessage(); die(); } } public function getall($sql, array $params) { $stmt = $this->conn->prepare($sql); $stmt->execute($params); return $stmt->fetch(pdo::fetch_assoc); } }
generic_file.php
require_once '../includes/config.php'; $dbh = new database(); $sql = ("select * users id = :id , username = :username"); $id = 1; $username = 'craig'; $params = array(':id' => $id, ':username' => $username); $row = $dbh->getall($sql, $params); var_dump($row);
now, me seems totally pointless. using pdo alone, not wrapper class, query simple write. on top of - if want use different fetch methods - i'd have write more methods in class. also, have used simple function instantiate , return new pdo object, including file , assigning variable function return simple quick - again feel class method overkill.
also, code above, , doing in class, not losing benefit of 'preparing' statement have pass sql statement in each time, change variables same statement?
however, find lots of examples online, , on lynda.com using currently, of database wrapper classes. on top of - no expert, , therefore feel overkill may best practice , recommended, hence looking experts me out!
so...back question - there reason use such class when using pdo? if not, there dry method others use minimize lines of codes needed queries using pdo?
thanks in advance.
i had same question @ 1 time.
the benefit abstracting database away can assure connections made correctly , if ever need change type of database there 1 spot need change code for. makes easier check queries issued because know if echo , exit in class of queries able checked.
the way solved creating class constructer established connection , assigned private variable while setting table in database too. best way have few public functions create, retrieve, update, , delete. called crud. each function , first parameter array. creating takes array , creates prepared statement , executes it. similar thing others retrieve array being matched, update takes things ending in id , sets rest update id = provided, , delete deletes of keys = value in table.
edit: here delete function put in class. if 1 of parameter's values array prepare statement , cycle through it. 1 variable changing though. have pass array values of numerical indexes being array of want insert though not how set code.
public function delete($info) { $dbh = $this->dbh; if (isset($info['submit_action'])) unset($info['submit_action']); $where = array(); foreach (array_keys($info) $name) { $where[] .= "$name = :$name"; } //echo "delete {$this->table_name} " . implode(" , ", $where) . ";"; exit; $data = $dbh->prepare("delete {$this->table_name} " . implode(" , ", $where) . ";"); foreach ($info $name => $value) { if ($array_value == $name) $data->bindparam(":$name", $array_info); else $data->bindvalue(":$name", trim($value)); } foreach ($info $name => $value) if (is_array($value)) { $array_value = $name; break; } if (isset($array_value)) { foreach ($info[$array_value] $array_info) { try { $data->execute(); } catch (pdoexception $e) { if (!is_null($this->error_msg)) handle_error($this->error_msg, $e->getmessage()); else handle_error("there problem removing {$this->subject}.", $e->getmessage()); } } } else { try { $data->execute(); } catch (pdoexception $e) { handle_error(/*public error msg - set anyway want*/, $e->getmessage()); } } // send success msg }
Comments
Post a Comment