Php Mysql PDO
程序员文章站
2022-06-09 22:07:27
...
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)!
推荐阅读
-
mysql 5.7以上版本安装配置方法图文教程(mysql 5.7.12mysql 5.7.13mysql 5.7.14)
-
Mysql5.7.11在windows10上的安装与配置(解压版)
-
mysql 5.7.14 安装配置简单教程
-
Centos6.5编译安装mysql 5.7.14详细教程
-
Linux上通过binlog文件恢复mysql数据库详细步骤
-
Window10下安装 mysql5.7图文教程(解压版)
-
mysql 5.7如何安装 mysql 5.7安装配置教程
-
如何将MySQL的两个表名对调
-
MYSQL建立外键失败几种情况记录Can't create table不能创建表
-
MySQL中CHAR和VARCHAR类型演变和详解