<?php
header(
"content-type:text/html;charset=utf-8"
);
class
db{
private
static
$dbcon
=false;
private
function
__construct(){
$dbcon
=@mysql_connect(
"localhost"
,
"root"
,
"root"
);
mysql_select_db(
"small2"
,
$dbcon
)
or
die
(
"mysql_connect error"
);
mysql_query(
"set names utf8"
);
}
private
function
__clone(){}
public
static
function
getIntance(){
if
(self::
$dbcon
==false){
self::
$dbcon
=
new
self;
}
return
self::
$dbcon
;
}
public
function
p(
$arr
){
echo
"<pre>"
;
print_r(
$arr
);
echo
"</pre>"
;
}
public
function
v(
$arr
){
echo
"<pre>"
;
var_dump(
$arr
);
echo
"</pre>"
;
}
public
function
query(
$sql
){
$query
=mysql_query(
$sql
);
return
$query
;
}
public
function
getOne(
$sql
){
$query
=
$this
->query(
$sql
);
return
mysql_result(
$query
,0);
}
public
function
getRow(
$sql
,
$type
=
"assoc"
){
$query
=
$this
->query(
$sql
);
if
(!in_array(
$type
,
array
(
"assoc"
,
'array'
,
"row"
))){
die
(
"mysql_query error"
);
}
$funcname
=
"mysql_fetch_"
.
$type
;
return
$funcname
(
$query
);
}
public
function
getFormSource(
$query
,
$type
=
"assoc"
){
if
(!in_array(
$type
,
array
(
"assoc"
,
"array"
,
"row"
)))
{
die
(
"mysql_query error"
);
}
$funcname
=
"mysql_fetch_"
.
$type
;
return
$funcname
(
$query
);
}
public
function
getAll(
$sql
){
$query
=
$this
->query(
$sql
);
$list
=
array
();
while
(
$r
=
$this
->getFormSource(
$query
)) {
$list
[]=
$r
;
}
return
$list
;
}
public
function
getInsertid(){
return
mysql_insert_id();
}
public
function
insert(
$table
,
$data
){
$key_str
=
''
;
$v_str
=
''
;
foreach
(
$data
as
$key
=>
$v
){
if
(
empty
(
$v
)){
die
(
"error"
);
}
$key_str
.=
$key
.
','
;
$v_str
.=
"'$v',"
;
}
$key_str
=trim(
$key_str
,
','
);
$v_str
=trim(
$v_str
,
','
);
$sql
=
"insert into $table ($key_str) values ($v_str)"
;
$this
->query(
$sql
);
return
mysql_insert_id();
}
public
function
deleteOne(
$table
,
$where
){
if
(
is_array
(
$where
)){
foreach
(
$where
as
$key
=>
$val
) {
$condition
=
$key
.
'='
.
$val
;
}
}
else
{
$condition
=
$where
;
}
$sql
=
"delete from $table where $condition"
;
$this
->query(
$sql
);
return
mysql_affected_rows();
}
public
function
deleteAll(
$table
,
$where
){
if
(
is_array
(
$where
)){
foreach
(
$where
as
$key
=>
$val
) {
if
(
is_array
(
$val
)){
$condition
=
$key
.
' in ('
.implode(
','
,
$val
) .
')'
;
}
else
{
$condition
=
$key
.
'='
.
$val
;
}
}
}
else
{
$condition
=
$where
;
}
$sql
=
"delete from $table where $condition"
;
$this
->query(
$sql
);
return
mysql_affected_rows();
}
public
function
update(
$table
,
$data
,
$where
){
$str
=
''
;
foreach
(
$data
as
$key
=>
$v
){
$str
.=
"$key='$v',"
;
}
$str
=rtrim(
$str
,
','
);
$sql
=
"update $table set $str where $where"
;
$this
->query(
$sql
);
return
mysql_affected_rows();
}
}
?>