php mysql案例_PHP+MYSQL-面向对象-操作数据库案例(转)

//定义封装的类:公共的数据库类

class MySQL{

private $host;//主机

private $name;//用户名

private $pass;//密码

private $database;//数据库

private $charset;//编码

private $conn;//连接

//用set get函数

function __set($n,$v){

$this->$n=$v;

}

function __get($n){

return

$this->$n;

}

//构造函数赋值

function

__construct($host,$name,$pass,$db,$cs){

$this->host=$host;

$this->name=$name;

$this->pass=$pass;

$this->database=$db;

$this->charset=$cs;

$this->conn=$this->getConn();

}

//得到链接

function getConn(){

$conn

= mysql_connect($this->host,$this->name,$this->pass)

or die("连接失败");

mysql_select_db($this->database,$conn)

or die("失败");

mysql_query("set names

'$this->charset'");

return $conn;

}

function myUpdate($sql){

$res =

mysql_query($sql,$this->conn) or

die(mysql_error());

if($res>0){

echo

"成功";

}else{

echo

"失败";

}

return $res;

}

function myFetch($result){

$row =

mysql_fetch_array($result);

return $row;

}

//吕老师教学视频-http://www.tudou.com/home/xuexi158

}

?>

include("08MySQL.php");

//创建mysql对象

$mysql = new

MySQL("localhost","root","123456","think_frank","gbk");

//添加

$addSQL = "insert into think_blog(title,content)

values('08网络','马上毕业了')";

$mysql->myUpdate($addSQL);

//删除

//$delSQL = "delete from think_blog where

id=19";

//$result = $mysql->myUpdate($delSQL);

//echo $result;

//修改

//$upSQL = "update think_blog set title='网络信息' ,content='毕业了真高兴'

where id=24";

//$r = $mysql->myUpdate($upSQL);

//echo $r;

//查询单条记录

$sql="select * from think_blog where id=16";

$res = $mysql->myUpdate($sql);

//需要对结果做遍历

if($row = $mysql->myFetch($res)){

echo

$row[0].",".$row[1].",".$row[2]."
";

}

查询所有记录

$allSQL = "select * from think_blog";

$resall = $mysql->myUpdate($allSQL);

while($row = $mysql->myFetch($resall)){

echo

$row[id].",".$row[title].",".$row[content]."
";

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值