使用创建测试库和表创建数据库db_test;CREATETABLE`user`(`id`int(10)unsignedNOTNULLAUTO_INCREMENT,`name`char(11)NOTNULL,`created_at`int(10)unsignedNOTNULL,PRIMARYKEY(`uid`))ENGINE=InnoDBDEFAULTCHARSET=utf8;INSERTINTO`user`VALUES('1','wang','1501109027');INSERTINTO`user`VALUES('2','meng','1501109026');INSERTINTO`user`VALUES('3','liu','1501009027');INSERTINTO`user`VALUES('4','yuan','1500109027');代码测试需要__DIR__。'/DB.php';$db=newDB();$db->__setup(['dsn'=>'mysql:dbname=db_test;host=localhost','用户名'=>'root','密码'=>'******','charset'=>'utf8']);$user=$db->fetch('SELECT*FROMuserwhereid=:id',['id'=>1]);echo$user['name'];echo"\n";$insertId=$db->insert('user',['name'=>'salamander','created_at'=>time()]);echo"插入用户{$insertId}\n";$users=$db->fetchAll('SELECT*FROMuser');foreach($usersas$item){echo"user{$item['id']}is{$item['name']}\n";}运行结果数据库工具类dsn=$config['dsn'];$this->user=$config['用户名'];$this->password=$config['密码'];$this->charset=$config['charset'];$this->connect();}privatefunctionconnect(){if(!$this->dbh){$options=array(\PDO::MYSQL_ATTR_INIT_COMMAND=>'SETNAMES'.$this->charset,);$this->dbh=new\PDO($this->dsn,$this->user,$this->password,$options);}}publicfunctionbeginTransaction(){return$this->dbh->beginTransaction();}民众函数inTransaction(){返回$this->dbh->inTransaction();}publicfunctionrollBack(){return$this->dbh->rollBack();}publicfunctioncommit(){return$this->dbh->commit();}functionwatchException($execute_state){if(!$execute_state){thrownewMySQLException("SQL:{$this->lastSQL}\n".$this->sth->errorInfo()[2],intval($这个->某事->errorCode()));}}publicfunctionfetchAll($sql,$parameters=[]){$result=[];$this->lastSQL=$sql;$this->sth=$this->dbh->prepare($sql);$this->watchException($this->sth->execute($parameters));while($result[]=$this->sth->fetch(\PDO::FETCH_ASSOC)){}array_pop($result);返回$结果;}publicfunctionfetchColumnAll($sql,$parameters=[],$position=0){$result=[];$this->lastSQL=$sql;$this->sth=$this->dbh->prepare($sql);$this->watchException($this->sth->execute($parameters));while($result[]=$this->sth->fetch(\PDO::FETCH_COLUMN,$position)){}array_pop($result);返回$结果;}publicfunctionexists($sql,$parameters=[]){$this->lastSQL=$sql;$data=$this->fetch($sql,$parameters);返回!空($数据);}publicfunctionquery($sql,$parameters=[]){$this->lastSQL=$sql;$this->sth=$this->dbh->prepare($sql);$this->watchException($this->sth->execute($parameters));返回$this->sth->rowCount();}publicfunctionfetch($sql,$parameters=[],$type=\PDO::FETCH_ASSOC){$this->lastSQL=$sql;$this->sth=$this->dbh->prepare($sql);$this->watchException($this->sth->execute($parameters));r返回$this->sth->fetch($type);}publicfunctionfetchColumn($sql,$parameters=[],$position=0){$this->lastSQL=$sql;$this->sth=$this->dbh->prepare($sql);$this->watchException($this->sth->execute($parameters));返回$this->sth->fetch(\PDO::FETCH_COLUMN,$position);}publicfunctionupdate($table,$parameters=[],$condition=[]){$table=$this->format_table_name($table);$sql="更新$tableSET";$字段=[];$pdo_parameters=[];foreach($parametersas$field=>$value){$fields[]='`'.$field.'`=:field_'.$field;$pdo_parameters['field_'.$field]=$value;$sql.=内爆(',',$fields);$字段=[];$在哪里='';if(is_string($condition)){$where=$condition;}elseif(is_array($condition)){foreach($conditionas$field=>$value){$parameters[$field]=$value;$fields[]='`'.$field.'`=:condition_'.$field;$pdo_parameters['condition_'.$field]=$value;}$where=implode('AND',$fields);}if(!empty($where)){$sql.='WHERE'.$where;}返回$this->query($sql,$pdo_parameters);}publicfunctioninsert($table,$parameters=[]){$table=$this->format_table_name($table);$sql="插入$table";$字段=[];$占位符=[];foreach($parametersas$field=>$value){$placeholder[]=':'.$field;$fields[]='`'.$field.'`';}$sql.='('.implode(",",$fields).')VALUES('.implode(",",$placeholder).')';$this->lastSQL=$sql;$this->sth=$this->dbh->prepare($sql);$this->watchException($this->sth->execute($parameters));$id=$this->dbh->lastInsertId();如果(空($id)){返回$this->sth->rowCount();}else{返回$id;}}publicfunctionerrorInfo(){return$this->sth->errorInfo();}protectedfunctionformat_table_name($table){$parts=explode(".",$table,2);if(count($parts)>1){$table=$parts[0].".`{$parts[1]}`";}else{$table="`$table`";}返回$表;}functionerrorCode(){return$this->sth->errorCode();}}}classMySQLExceptionextends\Exception{}Composer安装并使用SimpleDB框架。推荐使用框架中的DB类,最好使用单例模式或者依赖容器进行管理。
