Php Mysql PDO
程序员文章站
2024-01-19 21:01:40
...
Php Mysql PDO
<?php header("Content-type:text/html; charset=utf8"); class Mysql{ protected $mysql; function __construct(){ $this->mysql=new PDO("mysql:host=localhost;dbname=mytest","root","root"); if(!$this->mysql) { throw new Exception("Can't connect to Mysql");exit(0);} $this->mysql->query("set names utf8"); } function getItem($id){ $result=$this->mysql->prepare("select * from table01 where id=:id"); $result->bindParam(':id',$id,PDO::PARAM_INT); //bindValue:不接受php参数 $result->execute(); $resultArray=array(); while($row=$result->fetch(PDO::FETCH_ASSOC)){ array_push($resultArray,array($row['number'],$row['name'])); } return $resultArray; } function removeItem($name){ $delete=$this->mysql->prepare("delete from table01 where name=:name"); $delete->bindParam(':name',$name,PDO::PARAM_STR); $delete->execute(); if($delete) return true; else return false; } function addItem($number,$name){ $insert=$this->mysql->prepare("insert into table01(number,name) values (:number,:name)"); $insert->bindParam(':number',$number,PDO::PARAM_INT); $insert->bindParam(':name',$name,PDO::PARAM_STR); $insert->execute(); if($insert) return true; else return false; } } try{ $mysql=new Mysql(); //添加条目 if($mysql->addItem(5,"five")) echo "addItem(5,'five') is success<br>"; else echo "addItem(5,'five') is wrong<br>"; //删除条目 if($mysql->removeItem("five")) echo "removeItem('five') is success<br>"; else echo "removeItem('five') is wrong<br>"; //查找条目 $result=$mysql->getItem(1); echo $result[0][0]."<br>".$result[0][1]; }catch(Exception $e){ echo $e->getMessage()."<br>"; }
以上就是Php Mysql PDO的内容,更多相关内容请关注PHP中文网(www.php.cn)!
上一篇: MariaDB10之TokuDB存储引擎