Maison > php教程 > PHP源码 > le corps du texte

数据库操作原函数定制数据库操作手脚架,类

PHP中文网
Libérer: 2016-05-25 16:58:18
original
1142 Les gens l'ont consulté

1. [代码]db.php    

<?php
@header(&#39;Content-type:text/html;charset=UTF-8&#39;);
$pdo=new PDO("mysql:host=localhost;dbname=log;charset=utf8","root","root",array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING))or die(print_r($pdo->errorInfo(),true));
?>
Copier après la connexion

2. [代码]db.function.php

<?php
@header(&#39;Content-type:text/html;charset=UTF-8&#39;);
//	if (!defined("ACCESS"))
//	exit("Access Denied!");
	
require "db.php";//$pdo
/* db()函数,简化了数据库操作.insert update select count delete ,视情况添加max,avg,min 适于自己包装
$table=table name,$method=db action (crud,count(*),max/min/avg/($field)) , $field=column name or (array)names, $value=field value or (array)values ,$where=where clause, +orderby,+limit,it is optional.
last,use native $sql=sql sentence;
*/	
function db($table=&#39;&#39;,$method=&#39;&#39;,$field=&#39;&#39;,$value=&#39;&#39;,$where=&#39;&#39;,$return=&#39;&#39;,$sql=&#39;&#39;)
{
	global $pdo;var_dump($where);
		if($where==&#39;&#39;){$tail=&#39;&#39;;}else{$tail=&#39;where &#39;.$where;var_dump($tail);}//若是不带where的小尾巴,就写(id>0)+tail
	switch($method)
	{
		case "insert": //db($table,$field,$value) insert into $table ($fields) values ($values)
			$sql="insert into {$table} {$field} values {$value}";break; 
		case "update": //db($table,field,$value) update $table set ($fields) values ($values) where ($where)
			$sql="update {$table} set {$field} values {$value} {$tail}";break;  
		case "select": //db($table,$field,$where) select ($fields) from $table where ($where)
			$sql="select {$field} from {$table} {$tail}";break;  
		case "count": //db($table,$where) select count(*) from $table where ($where)
			$sql="select count(*) from {$table} {$tail}";break;  
		case "delete": //db($table,$where) delete from $table where ($where)
			$sql="delete from {$table} $tail";break;  
		default: // db($table,$method,$where) $method=(count(*),max/min/avg/($field)) $method="count(*)" select count(*) from $table where id<10
			$sql="select {$method} ({$field}) from {$table} {$tial}";break; //you can change sql to $sql="select {$method} from {$table} $tail"  to get more free
	}

	$db=$pdo->prepare($sql);
	$db->execute();

	switch($return)
	{
		case "id":$return=$db->lastInsertId;break; //返回最后影响id
		case "rows":$return=$db->rowCount();break; //返回影响行数
		case "row":$return=$db->fetch();break;  //返回行记录
		case "allrow":$return=$db->fetchAll();break;  //返回所有行记录
		default:break;		
	}
	var_dump($sql); //you can change it to $return[&#39;sql&#39;]=$sql;
	var_dump($return);
	return
		$return;
}
/*用例
$array=db("user","select","username",&#39;&#39;,"userid<10","allrow");
print_r($array);

	it is short for:
$sql="select usrname from user where userid<10";
$db=$pdo->prepare($sql);
$db->execute();
return $db->fetchAll();

*/

//简洁独立函数

function insert($table,$field=&#39;&#39;,$value=&#39;&#39;,$return=&#39;&#39;){return db($table,"insert",$field,$value,&#39;&#39;,$return);}  //db->insert($table,$field,$value,$return)
function update($table,$field=&#39;&#39;,$value=&#39;&#39;,$where=&#39;&#39;,$return=&#39;&#39;) {return db($table,"update",$field,$value,$where,$return);}
//function countRows($table,$field=&#39;*&#39;,$where=&#39;&#39;,$return=&#39;rows&#39;) {return db($table,&#39;count&#39;,$field,&#39;&#39;,$where,$return);} 跟php count()冲突
function select($table,$field=&#39;&#39;,$where=&#39;&#39;,$return=&#39;&#39;) {return db($table,"select",$field,&#39;&#39;,$where,$return);}  //db->select($table,$field,$where=&#39;&#39;)
function delete($table,$where,$return=&#39;&#39;) {return db($table,"delete",&#39;&#39;,&#39;&#39;,$where,$return);}  //db->delete($table,$where)
function execute($sql,$return=&#39;&#39;) {return db(&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,$return,$sql);}  //$db->prepare($sql,$return)

$array=select(&#39;user&#39;,&#39;username&#39;,&#39;userid<10&#39;,&#39;allrow&#39;);
var_dump($array);
?>
Copier après la connexion

3. [代码]db.class.php

<?php
	@header(&#39;Content-type:text/html;charset=UTF-8&#39;);
//	if (!defined("ACCESS"))
//	exit("Access Denied!");
/*
require "db.function.php";
class db{
	
	global $pdo;
	$table;
	$field;
	$value;
	$return;
	$sql;

	function __construct($table=&#39;&#39;,$field=&#39;&#39;,$value=&#39;&#39;,$where=&#39;&#39;,$return=&#39;&#39;,$sql=&#39;&#39;)
	{
		$this->table=$table;
		$this->field=$field;
		$this->value=$value;
		$this->return=$return;
		$this->where=$where;
		$this->sql=$sql;	
	}
	
	function insert($table,$field=&#39;&#39;,$value=&#39;&#39;,$return=&#39;&#39;){$db($table,"insert",$field,$value,&#39;&#39;,$return);}  //$db->insert($table,$field,$value,$return)
	function update($table,$filed=&#39;&#39;,$value=&#39;&#39;,$where,$return=&#39;&#39;) {$db($table,"update",&#39;&#39;,$value,$return);}
	function select($table,$filed=&#39;&#39;,$where=&#39;&#39;,$return=&#39;&#39;) {$db($table,"select",&#39;&#39;,$value,$return);}  //$db->select($table,$field,$where=&#39;&#39;)
	function count($table,$where=&#39;&#39;,$return=&#39;&#39;) {$db($table,"count",&#39;,&#39;$value,$return);}  //$db->count($table,$field,$where=&#39;&#39;)
	function delete($table,$where,$return=&#39;&#39;) {$db($table,"delete",&#39;&#39;,$value,$return);}  //$db->delete($table,$where)
	function prepare($sql,$return=&#39;&#39;) {$db(&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,$return,$sql);}  //$db->prepare($sql,$return)
}

/*$metadb=new db($pdo);
$array=db("user","select","name",&#39;&#39;,"userid<10","allrow");
print_r($array);
*/
*/

?>
Copier après la connexion

4. [代码]user.function.php

<?php
@header(&#39;Content-type:text/html;charset=UTF-8&#39;);
	$time=date(&#39;Y-m-d H:i:s&#39;);
	$fields=(usernaem,password,server_password,tel,email,ip,profile,addtime);
	$values=($username,$password,$server_password,$tel,$email,$ip,$profile,$time);
//$fields 字段数组
//$values 值数组
function addUser($values){
	$fields=(usernaem,password,server_password,tel,email,ip,profile,addtime);
//	$values=($username,$password,$server_password,$tel,$email,$ip,$profile,date(&#39;Y-m-d H:i:s&#39;));
	return insert(&#39;user&#39;,$fields,$values,&#39;rows&#39;);
}
function setUser($inputs){
	return update(&#39;user&#39;,$fields,$values,&#39;userid={$userid}&#39;,&#39;rows&#39;);
}
function getUser($userid){
	return select(&#39;user&#39;,&#39;*&#39;,id={$userid}&#39;,&#39;allrow&#39;);
}

?>
Copier après la connexion

                   

                   

Étiquettes associées:
php
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal