实例
<?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'); ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例