<?php
define('PEAR_MDB2_DIR',$_SERVER['DOCUMENT_ROOT'].'/src/php/pear/MDB2/');
require_once(PEAR_MDB2_DIR.'MDB2.php');
class pearMDB2Model {
//メンバ変数
protected $db_dsn = array(
'phptype' => 'mysqli',
'username' => 'testuser',
'password' => 'testpass',
'hostspec' => 'localhost',
'database' => 'testdb'
);
protected $db='';
protected $res='';
protected $sql_insert='';
protected $sql_update='';
protected $sql_select='';
//コンストラクタ
public function __construct(){
//接続
ini_set('include_path',PEAR_MDB2_DIR.PATH_SEPARATOR.ini_get('include_path'));
$this->db= & MDB2::singleton($this->db_dsn,'charset=utf8');
if (PEAR::isError($this->db)) {
die($this->db->getMessage());
}
$this->db->setFetchMode(MDB2_FETCHMODE_ASSOC);
$this->db->query('SET NAMES UTF8');
//insert文の用意
$sql_insert =' INSERT INTO books ';
$sql_insert.=' (name,author,publisher,price,isbn,date,comment,created_at) ';
$sql_insert.=' values ';
$sql_insert.=' ( :name , :author , :publisher , :price , :isbn , :date , :comment , now() )';
$this->sql_insert=$this->db->prepare($sql_insert);
//update文の用意
$sql_update ='UPDATE books SET ';
$sql_update.=' name= :name ';
$sql_update.=', author= :author ';
$sql_update.=', publisher= :publisher ';
$sql_update.=', price= :price ';
$sql_update.=', isbn= :isbn ';
$sql_update.=', date= :date ';
$sql_update.=', comment=:comment ';
$sql_update.=', updated_at=now() ';
$sql_update.=' where id= :id ';
$this->sql_update=$this->db->prepare($sql_update);
//select文の用意
$sql_select =' SELECT * FROM books ';
$sql_select.=' WHERE ';
$sql_select.=' name LIKE :name ';
$sql_select.='and author LIKE :author ';
$sql_select.='and publisher LIKE :publisher ';
$sql_select.='and price LIKE :price ';
$sql_select.='and isbn LIKE :isbn ';
$sql_select.='and date LIKE :date ';
$sql_select.='and comment LIKE :comment ';
$this->sql_select=$this->db->prepare($sql_select);
}
//インサート
public function insert_prepare($sql_array){
$res=$this->sql_insert->execute($sql_array);
if (PEAR::isError($this->res)){
return false;
}else{
return $this->db->lastInsertId();
}
}
//セレクト
public function select_prepare($sql_array){
if(isset($sql_array['name'])) {$sql_array['name']='%'.$sql_array['name'].'%'; }else{$sql_array['name']='%%';}
if(isset($sql_array['author'])) {$sql_array['author']='%'.$sql_array['author'].'%'; }else{$sql_array['author']='%%';}
if(isset($sql_array['publisher'])) {$sql_array['publisher']='%'.$sql_array['publisher'].'%'; }else{$sql_array['publisher']='%%';}
if(isset($sql_array['price'])) {$sql_array['price']='%'.$sql_array['price'].'%'; }else{$sql_array['price']='%%';}
if(isset($sql_array['isbn'])) {$sql_array['isbn']='%'.$sql_array['isbn'].'%'; }else{$sql_array['isbn']='%%';}
if(isset($sql_array['comment'])) {$sql_array['comment']='%'.$sql_array['comment'].'%'; }else{$sql_array['comment']='%%';}
$res=$this->sql_select->execute($sql_array);
$res=$res->fetchAll();
if (PEAR::isError($this->res)){
return false;
}else{
return $res;
}
}
//アップデート
public function update_prepare($id,$sql_array){
$sql_array['id']=$id;
$res=$this->sql_update->execute($sql_array);
if (PEAR::isError($this->res)){
return false;
}else{
return $this->db->lastInsertId();
}
}
//プリペアードステートメント使わないセレクト
public function select($sql_array){
$sql =' select * from books ';
$sql.=' where deleted_at is null ';
foreach($sql_array as $key=>$val){
$sql.=" and ".$key." like ".$this->db->quote('%'.$val.'%','text');
}
$res=$this->db->query($sql);
$res=$res->fetchAll();
if (PEAR::isError($this->res)){
return false;
}else{
return $res;
}
}
//プリペアードステートメント使わないアップデート
public function update($id,$sql_array){
$sql =' update books set update_date=now(), ';
foreach($sql_array as $key=>$val){
$sql.=$key."=".$this->db->quote($val,'text')." , ";
}
$sql.=' where id='.$this->db->quote($id,'text');
$res=$this->db->query($sql);
$res=$res->fetchAll();
if (PEAR::isError($this->res)){
return false;
}else{
return $res;
}
}
#↓クラスのおわり
}
|
http://yuubiseiharukana.blog.shinobi.jp/Entry/78/
>「無理に使う必要はないかもしれません」なんてことを書いているわけですが、忘れてください。
>「可能な限り使用しなければなりません」に修正します。