php 求一个mysqli的db类注释尽可能的多,初学小白

php 求一个mysqli的db类注释尽可能的多,初学小白,第1张

mysqli一个最简单的例子,要深入封装的话可以自己再增加...

其实个人觉得mysqli已经没什麼必要封装了.....

<?php

class db{ //类名

public $con //定义句柄

public $result //结果存取

public function __construct($Host,$User,$Pass,$DB){ //构建函数

$this->con = new mysqli($Host,$User,$Pass,$DB) //调用mysqli类

if($this->con->connect_error){ //判断是否有错误,有错误则返回连接错误代号和错误内容

return array($this->con->connect_errno,$this->con->connect_error)

}

}

public function query($sql,$type=''){ //执行查询,$sql为查询语句,$type为result mode [MYSQLI_USE_RESULT ] OR [MYSQLI_STORE_RESULT ] 执行成功返回true,否则返回false

$this->result = empty($type) ? $this->con->query($sql) : $this->con->query($sql,$type)

return !$this->result ? false : true

}

public function insertid(){ //必须先进行query才能获得插入或更新的id

return $this->con->insert_id

}

public function fetch($n,$t){//获取结果集,$n必选[array][assoc][field_direct][field][fields][object][row][all],$t为$n对应的可选参数,成功返回结果集

$f = 'fetch_'.$n

return $this->result->$f($t)

}

public function __destruct(){ //销毁函数

if($this->result)$this->result->close()

if($this->con)$this->con->close()

}

public function GetError(){ //获取错误

return array($this->con->errno,$this->con->error)

}

}

$db = new db('127.0.0.1','','','test')

if(!$db->query("insert into tb (`time`,`amount`)values('1420085532','300')")){

var_dump($db->GetError()) 

die()

}

echo $db->insertid(),PHP_EOL

$db->query('select * from tb')

while($arr = $db->fetch('array',MYSQLI_NUM)){

echo $arr['0'],'      ',$arr['1'],'      ',$arr['2'],'      ',PHP_EOL

}

如果你当前已经有现成的DB类来 *** 作数据库,那么就不建议你去改一个开源框架或者现成系统的DB层,MySQL提供了一个代理叫做MySQL Proxy完全可以满足你的要求而且不需要改动任何代码,它会自动识别MySQL实例访问的SQL是读 *** 作还是写 *** 作。

sql server 2005里面的tinyint 长度为一个字节 相当于c#中的byte类型其范围为0到255所以转换过程为 string str="123"byte byten=Convert.ToByte(str)这样byte就和数据库里面的tinyint对应上了


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/6136787.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-16
下一篇 2023-03-16

发表评论

登录后才能评论

评论列表(0条)

保存