php mysql 数据库类_php MySQL数据库类

[php]代码库<?php

/*

More & Original PHP Framwork

Copyright (c) 2007 - 2008 IsMole Inc.

$Id: Mysql.Class.php 2008-3-19 aming$

*/

!defined('IN_MOOPHP') && exit('Access Denied');

class MooMySQL

{

var $queryCount = 0;

var $conn;

var $result;

var $rsType = MYSQL_ASSOC;

//note:查询时间

var $queryTimes = 0;

/**

* 连接数据库

*

* @param string $dbhost

* @param string $dbdata

* @param string $dbuser

* @param string $dbpass

* @param blooean $dbOpenType

* @param string $dbcharset

*/

function connect($dbhost = '', $dbuser = '', $dbpass = '', $dbdata = '', $dbOpenType = false ,$dbcharset = 'utf8') {

if($dbOpenType) {

$this->conn = mysql_pconnect($dbhost, $dbuser, $dbpass) or die(mysql_errno()." : ".mysql_error());

} else {

$this->conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_errno()." : ".mysql_error());

}

$mysql_version = $this->getMysqlVersion();

if($mysql_version > '4.1') {

global $charset, $dbcharset;

$dbcharset = $dbcharset2 ? $dbcharset2 : $dbcharset;

$dbcharset = !$dbcharset && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8')) ? str_replace('-', '', $charset) : $dbcharset;

$serverset = $dbcharset ? 'character_set_connection='.$dbcharset.', character_set_results='.$dbcharset.', character_set_client=binary' : '';

$serverset .= $mysql_version > '5.0.1' ? ((empty($serverset) ? '' : ',').'sql_mode=\'\'') : '';

$serverset && mysql_query("SET $serverset", $this->conn);

}

@mysql_select_db($dbdata, $this->conn);

}

/**

* 关闭数据库连接,当您使用持续连接时该功能失效

*

*/

function close() {

return mysql_close($this->conn);

}

/**

* 发送查询语句

*

* @param string $sql

* @param string $type

* @return blooean

*/

function query($sql, $type = "ASSOC") {

$this->rsType = $type != "ASSOC" ? ($type == "NUM" ? MYSQL_NUM : MYSQL_BOTH) : MYSQL_ASSOC;

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

$this->queryCount++;

if($this->result) {

return $this->result;

} else {

return false;

}

}

/**

* 数据量比较大的情况下查询

*

* @param string $sql

* @param string $type

* @return blooean

*/

function bigQuery($sql, $type = "ASSOC") {

$this->rsType = $type != "ASSOC" ? ($type == "NUM" ? MYSQL_NUM : MYSQL_BOTH) : MYSQL_ASSOC;

$this->result = mysql_unbuffered_query($sql, $this->conn);

$this->queryCount++;

if($this->result) {

return $this->result;

}

else {

return false;

}

}

/**

* 获取全部数据

*

* @param string $sql

* @param blooean $nocache

* @return array

*/

function getAll($sql = "", $nocache = false) {

if($sql) {

if($nocache) {

$this->bigQuery($sql);

} else {

$this->query($sql);

}

}

$rows = array();

while($row = mysql_fetch_array($this->result, $this->rsType)) {

$rows[] = $row;

}

return $rows;

}

/**

* 获取单行数据

*

* @param string $sql

* @return array

*/

function getOne($sql = "") {

if($sql) {

$this->query($sql);

}

$rows = mysql_fetch_array($this->result, $this->rsType);

return $rows;

}

/**

* mysql_fetch_array

*

* @param string $sql

* @return

*/

function fetchArray($query) {

return mysql_fetch_array($query, $this->rsType);

}

/**

* 取得上一步 INSERT 操作产生的 ID

*

* @param string $sql

* @return integer

*/

function insertId($sql = "") {

if($sql) {

$row = $this->getOne($sql);

return $row;

} else {

return mysql_insert_id($this->conn);

}

}

function insert($sql) {

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

$id = $this->insertId();

return $id;

}

/**

* 取得行的数目

*

* @param string $sql

* @return integer

*/

function numRows($sql = "") {

if($sql) {

$this->query($sql);

unset($sql);

}

$row = mysql_num_rows($this->result);

return $row;

}

/**

* 取得结果集中字段的数目

*

* @param string $sql

* @return integer

*/

function numFields($sql = "") {

if($sql) {

$this->query($sql);

}

return @mysql_num_fields($this->result);

}

/**

* 取得结果中指定字段的字段名

*

* @param string $data

* @param string $table

* @return array

*/

function listFields($data, $table) {

$row = @mysql_list_fields($data, $table, $this->conn);

$count = mysql_num_fields($row);

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

$rows[] = @mysql_field_name($row,$i);

}

return $rows;

}

/**

* 列出数据库中的表

*

* @return array

*/

function listTables($data) {

$query = mysql_list_tables($data);

$rows = array();

while($row = @mysql_fetch_array($query)) {

$rows[] = $row[0];

}

return $rows;

}

/**

* 取得表名

*

* @param string $table_list

* @param integer $i

* @return string

*/

function tableName($table_list, $i) {

return @mysql_tablename($table_list, $i);

}

/**

* 转义字符串用于查询

*

* @param string $char

* @return string

*/

function escapeString($char) {

if(!$char) {

return false;

}

return @mysql_escape_string($char);

}

/**

* 取得数据库版本信息

*

* @return string

*/

function getMysqlVersion() {

return mysql_get_server_info();

}

}

694748ed64b9390909c0d88230893790.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值