mysql类库php_php操作Mysql数据库类

php操作mysql数据库类

闲来无聊,自己写了一个php操作mysql数据库的类。

下边是具体代码,有一定php基础的都能看懂,注释不多写了,有疑问或者交流的可以联系!

class Db {

private $_host = 'localhost';

private $_port = 3306;

private $_user = 'root';

private $_password = '';

private $_database = '';

private $_encode = 'utf8';

private $_table = '';

public function setHost($value) {

$this->_host = $value;

}

public function setPort($value) {

$this->_port = $value;

}

public function setUser($value) {

$this->_user = $value;

}

public function setPassword($value) {

$this->_password = $value;

}

public function setDatebase($value) {

$this->_database = $value;

}

public function setEncode($value) {

$this->_encode = $value;

}

public function setTable($value) {

$this->_table = $value;

}

private function _connect() {

if (mysqli_connect($this->_host, $this->_user, $this->_password, $this->_database, $this->_port)) {

return mysqli_connect($this->_host, $this->_user, $this->_password, $this->_database, $this->_port);

} else {

return null;

}

}

public function query($sql) {

return mysqli_query($this->_connect(), $sql);

}

public function add($data = array()) {

$count = count($data);

$field = array_keys($data);

$value = array_values($data);

$f = '';

$v = '';

for ($index = 0; $index < $count; $index++) {

if ($index < $count-1) {

$f .= $field[$index].', ';

$v .= "'".$value[$index]."', ";

} else {

$f .= $field[$index];

$v .= $value[$index];

}

}

$sql = "INSERT INTO $this->_table ($f) VALUES ($v)";

$handle = $this->_connect();

mysqli_select_db($handle, $this->_database);

mysqli_query($handle, "SET NAMES '$this->_encode'");

$isSuccess = mysqli_query($handle, $sql);

if (false == $isSuccess) {

return false;

} else {

return true;

}

}

public function del($condition = '') {

$sql = "DELETE FROM $this->_table $condition";

$handle = $this->_connect();

mysqli_select_db($handle, $this->_database);

mysqli_query($handle, "SET NAMES '$this->_encode'");

$isSuccess = mysqli_query($handle, $sql);

if (false == $isSuccess) {

return false;

} else {

return true;

}

}

public function update($data, $condition = '') {

$count = count($data);

$field = array_keys($data);

$value = array_values($data);

$sql = "UPDATE $this->_table SET ";

for ($index = 0; $index < $count; $index++) {

if ($index < $count-1) {

$sql .= $field[$index].' = '."'".$value[$index]."', ";

} else {

$sql .= $field[$index].' = '."'".$value[$index]."'";

}

}

echo $sql;

$handle = $this->_connect();

mysqli_select_db($handle, $this->_database);

mysqli_query($handle, "SET NAMES '$this->_encode'");

$isSuccess = mysqli_query($handle, $sql);

if (false == $isSuccess) {

return false;

} else {

return true;

}

}

public function find($condition = '') {

$sql = "SELECT * FROM $this->_table ".$condition;

echo $sql;

$handle = $this->_connect();

mysqli_select_db($handle, $this->_database);

mysqli_query($handle, "SET NAMES '$this->_encode'");

$tempresult = mysqli_query($handle, $sql);

$result = array();

while($row = mysqli_fetch_array($tempresult)) {

$result[] = $row;

}

return $result;

}

public function getItemCount($condition = '') {

$sql = "SELECT * FROM $this->_table ".$condition;

$handle = $this->_connect();

mysqli_select_db($handle, $this->_database);

mysqli_query($handle, "SET NAMES '$this->_encode'");

$result = mysqli_query($handle, $sql);

return mysqli_num_rows($result);

}

}

?>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值