Blogger Information
Blog 17
fans 1
comment 1
visits 16375
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
简单增删改查
雷国恩的博客
Original
753 people have browsed it

 

实例  简单增删改查

<?php
	function con(){
		$dsn = 'mysql:host=127.0.0.1;dbname=blog;charset=utf8;port=3306';
		$dbname = 'root';
		$dbpw = 'root';
		$p = new PDO($dsn,$dbname,$dbpw);
		return $p;
	}
	function insert($table,$column,$values){
		$c=con();
		$sql='';
		// $sql.="INSERT INTO `user` (`name`) values ('雷国恩')";
		$sql.='INSERT INTO '.$table.' ('.$column.') VALUES ('.$values.')';	
		$a=$c->prepare($sql);
		$a->execute();		
	}
	function update($table,$column,$values,$where){
		$c=con();
		$sql='';
		$sql.='UPDATE '.$table.' SET '.$column.' = '.$values.' WHERE '.$where;
		$a=$c->prepare($sql);
		$a->execute();
	}
    function delete($table,$where){
    	$c=con();
    	$sql='';
    	$sql.='DELETE FROM'.$table.' WHERE '.$where;
    	$a=$c->prepare($sql);
    	$a->execute();
    }
 function select($table,$field,$where='',$order='',$limit=''){
		$c=con();
	 // $sql='SELECT * FROM `user` where `age` > 40';
	 // $a=$c->prepare($sql);
	 // $a->execute();
	 // $a->setFetchMode(PDO::FETCH_ASSOC);
	 // $ret=$a->fetchALL();
	 // return $ret;

		$sql='SELECT ';
		if(empty($field)){
			$field='*';
		}
		$sql.= $field ;
		$sql.=' FROM ';
		$sql.= $table;
		if(!empty($where)){
			$sql.=' WHERE ';
			$sql.=$where;
		}
		if(!empty($order)){
			$sql.=' ORDER BY ';
			$sql.=$order;
		}
		if(!empty($limit)){
			$sql.=' LIMIT ';
			$sql.=$limit;
		}
	    $a=$c->prepare($sql);
	    $a->execute();
	   
	    $a->setFetchMode(PDO::FETCH_ASSOC);
	   	$ret=$a->fetchALL();

	   	return $ret;
	    
	}

// 查询
// $s=select('`user`','','`age`>40','`age` desc','2');
// print_r($s);
// print_r(select());
// 添加
	// $b=insert('`user`','`name` ,`sex`,`age`,`city`','"雷国恩","男",30,"重庆"');
	// print_r($b);
//修改
// update('`user`','`name`',"'保来'",'`id` = 32');
//删除
   // delete('`user`','`id` = 17');

运行实例 »

点击 "运行实例" 按钮查看在线实例

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!