1.用方法,实现PDO查询数库数据
实例
<?php function con() { $dsn='mysql:host=127.0.0.1;dbname=test;charset=utf8;posrt=3306'; $dbname='root'; $dbpwd='root'; try { $p=new PDO($dsn,$dbname,$dbpwd); } catch (PDOException $e) { print_r($e->getmessage()); exit; } return $p; } function select($feild='*',$table,$where='',$oder='',$limit=''){ $c=con(); $sql=' select ';//注意,前后有空格 if(!empty($feild)){ $sql .=rtrim($feild); } $sql .=' from ';//注意,前后有空格 $sql .=$table; if (!empty($where)) { $sql .=' where ';//注意,前后有空格 $sql .=$where; } if (!empty($oder)) { $sql .=' order by ';//注意,前后有空格 $sql .=$oder; } if (!empty($limit)) { $sql .=' limit ';//注意,前后有空格 $sql .=$limit; } $a=$c->prepare($sql); if ($a->execute()) { if ($a->rowcount()) { $a->setFetchMode(PDO::FETCH_ASSOC); $ret=$a->fetchall(); return $ret; }else{ return false; } }else{ return false; } } $s=select('`id`,`sex`,`name`','users','`age`>10','age','1,1'); print_r($s);
运行实例 »
点击 "运行实例" 按钮查看在线实例
2.使用自定义方法,实现PDO对数据库插入数据
实例
<?php function con(){ $dsn='mysql:host=127.0.0.1;dbname=test;charset=utf8;port=3306'; $dbname='root'; $dbpwd='root'; try { $p=new PDO($dsn,$dbname,$dbpwd); } catch (Exception $e) { print_r($e->getmessage()); exit; } return $p; } //INSERT into users(`name`,`sex`,`age`,`address`,`e-mail`) VALUES ('王五','男',33,'中国北京','22222@qq.com') function insert($table,$feild='*',$values){ $con=con(); $insert=' insert into '; if (!empty($table)) { $insert .=$table; } if (!empty($feild)) { $insert .=' ('; $insert .=rtrim($feild); $insert .=') '; } if (!empty($values)) { $insert .=' values '; $insert .=' ('; $insert .=$values; $insert .=') '; } $a=$con->prepare($insert); if ($a->execute()) { echo 'ok!'; }else{ return false; } } insert('users','`name`,`sex`,`age`,`address`,`e-mail`',"'李三','男','25','中国广东','121232@qq.com'"); ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
3.使用方法,实现PDO对数库数据进行更新
实例
<?php header("Content-Type: text/html;charset=utf-8"); function con(){ $dsn='mysql:host=127.0.0.1;dbname=test;charset=utf8;port=3306'; $dbname='root'; $dbpwd='root'; try { $p=new PDO($dsn,$dbname,$dbpwd); } catch (Exception $e) { print_r($e->getmessage()); } return $p; } //UPDATE users SET name='赵伟' where id=2 function update($table,$feild='*',$value,$feild1='*',$values){ $con=con(); $update='update '; if ($table) { $update .=$table; } if($feild){ $update .=' set '; $update .=rtrim($feild); } if ($value) { $update .='='; $update .=$value; } if ($feild1) { $update .=' where '; $update .=rtrim($feild1); } if ($values) { $update .='='; $update .=$values; } $a=$con->prepare($update); print_r($a); if ($a->execute()) { echo '更新成功!'; }else{ echo "更新不成功。"; } } update('users','name',"'大侠'",'id',4);
运行实例 »
点击 "运行实例" 按钮查看在线实例
4.使用自定义方法,实现PDO对数库数据进行删除
实例
<?php function con(){ $dsn='mysql:host=127.0.0.1;dbname=test;charset=utf8;port=3306'; $dbname='root'; $dbpwd='root'; try { $p=new PDO($dsn,$dbname,$dbpwd); } catch (Exception $e) { print_r($e->getmessage()); } return $p; } //DELETE from users where id=1 function delete($table,$feild='*',$values){ $con=con(); $delete='delete '; if (!empty($table)) { $delete .=' from '; $delete .=$table; } if (!empty($feild)) { $delete .=' where '; $delete .=$feild; } if (!empty($values)) { $delete .='='; $delete .=$values; } $a=$con->prepare($delete); if ($a->execute()) { echo '删除成功'; }else{ echo '删除失败。'; } } delete('users','id',4);
运行实例 »
点击 "运行实例" 按钮查看在线实例