mysql.class.php 使用_好用的mysql.class.php

/****

燕十八 公益PHP讲堂

defined('ACC')||exit('ACC Denied');

class mysql extends db {

private static $ins = NULL;

private $conn = NULL;

private $conf = array();

protected function __construct() {

$this->conf = conf::getIns();

$this->connect($this->conf->host,$this->conf->user,$this->conf->pwd);

$this->select_db($this->conf->db);

$this->setChar($this->conf->char);

}

public function __destruct() {

}

public static function getIns() {

if(!(self::$ins instanceof self)) {

self::$ins = new self();

}

return self::$ins;

}

public function connect($h,$u,$p) {

$this->conn = mysql_connect($h,$u,$p);

if(!$this->conn) {

$err = new Exception('连接失败');

throw $err;

}

}

protected function select_db($db) {

$sql = 'use ' . $db;

$this->query($sql);

}

protected function setChar($char) {

$sql = 'set names ' . $char;

return $this->query($sql);

}

public function query($sql) {

$rs = mysql_query($sql,$this->conn);

log::write($sql);

return $rs;

}

public function autoExecute($table,$arr,$mode='insert',$where = ' where 1 limit 1') {

/*    insert into tbname (username,passwd,email) values ('',)

/// 把所有的键名用','接起来

// implode(',',array_keys($arr));

// implode("','",array_values($arr));

*/

if(!is_array($arr)) {

return false;

}

if($mode == 'update') {

$sql = 'update ' . $table .' set ';

foreach($arr as $k=>$v) {

$sql .= $k . "='" . $v ."',";

}

$sql = rtrim($sql,',');

$sql .= $where;

return $this->query($sql);

}

$sql = 'insert into ' . $table . ' (' . implode(',',array_keys($arr)) . ')';

$sql .= ' values (\'';

$sql .= implode("','",array_values($arr));

$sql .= '\')';

return $this->query($sql);

}

public function getAll($sql) {

$rs = $this->query($sql);

$list = array();

while($row = mysql_fetch_assoc($rs)) {

$list[] = $row;

}

return $list;

}

public function getRow($sql) {

$rs = $this->query($sql);

return mysql_fetch_assoc($rs);

}

public function getOne($sql) {

$rs = $this->query($sql);

$row = mysql_fetch_row($rs);

return $row[0];

}

// 返回影响行数的函数

public function affected_rows() {

return mysql_affected_rows($this->conn);

}

// 返回最新的auto_increment列的自增长的值

public function insert_id() {

return mysql_insert_id($this->conn);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值