<?php
class
MySql {
private
$queryCount
= 0;
private
$conn
;
private
$result
;
private
static
$instance
= null;
private
function
__construct() {
if
(!function_exists('mysql_connect')) {
emMsg('服务器PHP不支持MySql数据库');
}
if
(!
$this
->conn = @mysql_connect(DB_HOST, DB_USER, DB_PASSWD)) {
emMsg(
"连接数据库失败,可能是数据库用户名或密码错误"
);
}
if
(
$this
->getMysqlVersion() > '4.1') {
mysql_query(
"SET NAMES 'utf8'"
);
}
@mysql_select_db(DB_NAME,
$this
->conn) OR emMsg(
"未找到指定数据库"
);
}
public
static
function
getInstance() {
if
(self::
$instance
== null) {
self::
$instance
=
new
MySql();
}
return
self::
$instance
;
}
function
close() {
return
mysql_close(
$this
->conn);
}
function
query(
$sql
) {
$this
->result = @mysql_query(
$sql
,
$this
->conn);
$this
->queryCount++;
if
(!
$this
->result) {
emMsg(
"SQL语句执行错误:$sql <br />"
.
$this
->geterror());
}
else
{
return
$this
->result;
}
}
function
fetch_array(
$query
,
$type
= MYSQL_ASSOC) {
return
mysql_fetch_array(
$query
,
$type
);
}
function
once_fetch_array(
$sql
) {
$this
->result =
$this
->query(
$sql
);
return
$this
->fetch_array(
$this
->result);
}
function
fetch_row(
$query
) {
return
mysql_fetch_row(
$query
);
}
function
num_rows(
$query
) {
return
mysql_num_rows(
$query
);
}
function
num_fields(
$query
) {
return
mysql_num_fields(
$query
);
}
function
insert_id() {
return
mysql_insert_id(
$this
->conn);
}
function
geterror() {
return
mysql_error();
}
function
affected_rows() {
return
mysql_affected_rows();
}
function
getMysqlVersion() {
return
mysql_get_server_info();
}
function
getQueryCount() {
return
$this
->queryCount;
}
}