<?php
defined(
'ACC'
)||
exit
(
'Access Denied'
);
class
mysql
extends
absdb{
protected
static
$ins
= null;
protected
$host
;
protected
$user
;
protected
$passwd
;
protected
$db
;
protected
$port
;
protected
$conn
= null;
public
static
function
getIns() {
if
(self::
$ins
=== null) {
self::
$ins
=
new
self();
}
$conf
= conf::getIns();
self::
$ins
->host =
$conf
->host;
self::
$ins
->user =
$conf
->user;
self::
$ins
->passwd =
$conf
->pwd;
self::
$ins
->db =
$conf
->db;
self::
$ins
->port =
$conf
->port;
self::
$ins
->connect();
self::
$ins
->select_db();
self::
$ins
->setChar();
return
self::
$ins
;
}
protected
function
__construct() {
}
public
function
connect() {
$this
->conn = @mysql_connect(
$this
->host,
$this
->user,
$this
->passwd,
$this
->port);
if
(!
$this
->conn) {
$error
=
new
Exception(
'数据库连不上'
,9);
throw
$error
;
}
}
public
function
query(
$sql
) {
$rs
= mysql_query(
$sql
,
$this
->conn);
if
(!
$rs
) {
log::write(
$sql
);
}
return
$rs
;
}
public
function
getAll(
$sql
) {
$rs
=
$this
->query(
$sql
);
if
(!
$rs
) {
return
false;
}
$list
=
array
();
while
(
$row
= mysql_fetch_assoc(
$rs
)) {
$list
[] =
$row
;
}
return
$list
;
}
public
function
getRow(
$sql
) {
$rs
=
$this
->query(
$sql
);
if
(!
$rs
) {
return
false;
}
return
mysql_fetch_assoc(
$rs
);
}
public
function
getOne(
$sql
) {
$rs
=
$this
->query(
$sql
);
if
(!
$rs
) {
return
false;
}
$tmp
= mysql_fetch_row(
$rs
);
return
$tmp
[0];
}
public
function
affected_rows() {
return
mysql_affected_rows(
$this
->conn);
}
public
function
last_id() {
return
mysql_insert_id(
$this
->conn);
}
public
function
select_db() {
$sql
=
'use '
.
$this
->db;
return
$this
->query(
$sql
);
}
public
function
setChar() {
$sql
=
'set names utf8'
;
return
$this
->query(
$sql
);
}
public
function
autoExecute(
$data
,
$table
,
$act
=
'insert'
,
$where
=
''
) {
if
(
$act
==
'insert'
) {
$sql
=
'insert into '
.
$table
.
' ('
;
$sql
.= implode(
','
,(
array_keys
(
$data
)));
$sql
.=
') values (\''
;
$sql
.= implode(
"','"
,
array_values
(
$data
));
$sql
.=
"')"
;
}
else
if
(
$act
==
'update'
) {
if
(!trim(
$where
)) {
return
false;
}
$sql
=
'update '
.
$table
.
' set '
;
foreach
(
$data
as
$k
=>
$v
) {
$sql
.=
$k
;
$sql
.=
'='
;
$sql
.=
"'"
.
$v
.
"',"
;
}
$sql
=
substr
(
$sql
,0,-1);
$sql
.=
' where '
;
$sql
.=
$where
;
}
else
{
return
false;
}
return
$this
->query(
$sql
);
}
}