Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class connect{
- public $host = "localhost";
- public $user = "root";
- public $password = "";
- public $db = "test2";
- public $charset = "utf8";
- public $pdo = "";
- public function __construct() {
- $dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset";
- $opt = array(
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
- );
- $this->pdo = new PDO($dsn, $this->user, $this->password, $opt);
- }
- }
- Class db extends connect{
- public function add($data)
- {
- $fields = $this->set_fields($data);
- $sql = "INSERT INTO `{$this->name_table}` SET".$fields;
- $stmt = $this->pdo->prepare( $sql );
- return $stmt->execute($data);
- }
- public function update($data, $where = "")
- {
- $fields = $this->set_fields($data);
- $sql = "UPDATE `{$this->name_table}` SET ".$fields;
- if(!empty($where)){
- $sql.= " WHERE ".$where;
- }
- $stmt = $this->pdo->prepare( $sql );
- return $stmt->execute($data);
- }
- public function set_fields( $items ){
- $str = array();
- foreach ($items as $key=>$item){
- $str[] = "`".$key."`=:".$key;
- }
- return implode(',', $str );
- }
- }
- Class Users extends db{
- public $name_table = "users";
- }
- $save = array("name" => "Владимир");
- $users = new Users();
- $res = $users->update($save, 'id=2');
- var_dump($res);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement