Table of Contents
1. [代码]db.php    
2. [代码]db.function.php    
3. [代码]db.class.php  
4. [代码]user.function.php  
Home php教程 PHP源码 数据库操作原函数定制数据库操作手脚架,类

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

May 25, 2016 pm 04:58 PM
php

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));
?>
Copy after login

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);
?>
Copy after login

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);
*/
*/

?>
Copy after login

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;);
}

?>
Copy after login

                   

                   

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles