Blogger Information
Blog 21
fans 0
comment 0
visits 19956
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
增删改方法
电动机的博客
Original
1005 people have browsed it

实例

<?php

function con(){
		$dsn = 'mysql:host=127.0.0.1;dbname=students;charset=utf8;port=3308';
		$dbname = 'root';
		$dbpw   = 'root';
		try{
			$p = new PDO($dsn,$dbname,$dbpw);
		} catch (PDOException $e) {
			print_r($e->getMessage());
			exit;
		}
		return $p;
	}
  

	// 新增数据
   function inser($user,$field,$value) {
   	$c=con();
   	// $sql='insert into user(`id`,`name`,`sex` ,`age`,`grade`,`techer`,`mobilphone`,`address`)  VALUES (9,333,0,10,33,22,12331,2222222)';
    $sql = ' insert into ';
    $sql .=  $user ;
    $sql .= ' ( ';
    $sql .= $field ;
    $sql .= ' ) ';
    $sql .= ' VALUES ';
    $sql .= ' ( ';
    $sql .= $value ;
    $sql .= ' ) ';
   $a = $c->prepare($sql);
    $a->execute();
   $a->setFetchMode(PDO::FETCH_ASSOC);
   }
   inser('user','`id`,`name`,`sex` ,`age`,`grade`,`techer`,`mobilphone`,`address`',"10,'li',0,10,33,'liu','13289841454','江苏。。。。。'");

   //删除
   function del($user,$value) {
   	$c=con(); 	   	
     $sql='delete ';
     $sql.=' from ';
     $sql.=$user;
     $sql.=' where ';
     $sql.=$value;
     $a = $c->prepare($sql);
    $a->execute();
   $a->setFetchMode(PDO::FETCH_ASSOC);
     }
    
   del('user','`id` in (9,10)');
   //更新
   function update($user,$field,$values){
      	$c=con();
      	$sql='update ';
      	$sql.=$user;
      	$sql.=' set ';
        $sql.=$field;
        $sql.=' where ';
        $sql.=$values;
        $a = $c->prepare($sql);
    $a->execute();
   $a->setFetchMode(PDO::FETCH_ASSOC);
          }
       	
    update('user','`age`=30','`id`>2');
   
?>

运行实例 »

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

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!