pdo php代码,Php Mysql PDO

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
";

else echo "addItem(5,'five') is wrong
";

//删除条目

if($mysql->removeItem("five")) echo "removeItem('five') is success
";

else echo "removeItem('five') is wrong
";

//查找条目

$result=$mysql->getItem(1);

echo $result[0][0]."
".$result[0][1];

}catch(Exception $e){

echo $e->getMessage()."
";

}

以上就是Php Mysql PDO的内容,更多相关内容请关注PHP中文网(www.php.cn)!

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值