search
Homephp教程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));
?>

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

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

?>

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

?>

                   

                   

Statement
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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.