<?php
class
Mysql
{
private
static
$object
;
private
$PDO
;
private
$prepare
;
private
function
__construct()
{
}
public
static
function
newClass()
{
if
(!(self::
$object
instanceof
self))
{
self::
$object
=
new
self;
}
return
self::
$object
;
}
public
function
__clone(){
trigger_error(
'Clone
is not allow!'
,E_USER_ERROR);
}
public
function
pdoConnect(
$address
)
{
try
{
$this
->PDO
=
new
PDO(
$address
[0],
$address
[1],
$address
[2]);
$this
->PDO->setAttribute(PDO::ATTR_PERSISTENT,true);
$this
->PDO->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$this
->PDO->setAttribute(PDO::ATTR_ORACLE_NULLS,true);
$this
->PDO->setAttribute(PDO::ATTR_EMULATE_PREPARES,
false);
}
catch
(PDOException
$e
)
{
$this
->Msg(
"PDO连接错误信息:"
.
$e
->getMessage());
}
}
private
function
Msg(
$the_error
=
""
){
$html
="<html>
<head>
<meta
http-equiv=
'Content-Type'
content=
'text/html;
charset=UTF-8'
/>
<title>mysql
error</title>
</head>
<body>
<div
style=
'width:
50%; height: 200px; border: 1px solid red; font-size: 12px;'
>
$the_error
</div>
</body>
</html>
";
echo
$html
;
exit
;
}
public
function
insert(
$table
,
$row
)
{
$sql
=
$this
->sqlCreate(
"insert"
,
$table
,
$row
);
$result
=
$this
->sqlExec(
$sql
);
}
public
function
update(
$table
,
$row
,
$where
)
{
$sql
=
$this
->sqlCreate(
"update"
,
$table
,
$row
,
$where
);
$result
=
$this
->sqlExec(
$sql
);
}
public
function
delete
(
$table
,
$where
)
{
$sql
=
$this
->sqlCreate(
"delete"
,
$table
,
""
,
$where
);
$result
=
$this
->sqlExec(
$sql
);
}
private
function
sqlCreate(
$action
,
$table
,
$row
=
""
,
$where
=
""
)
{
$actionArr
=
array
(
"insert"
=>
"insert
into "
,
"update"
=>
"update
"
,
"delete"
=>
"delete
from "
);
$row
=
empty
(
$row
)
?
""
:
$this
->rowCreate(
$row
);
$where
=
empty
(
$where
)
?
""
:
"
where "
.
$where
;
$sql
=
$actionArr
[
$action
].
$table
.
$row
.
$where
;
return
$sql
;
}
private
function
rowCreate(
$row
)
{
$sql_row
=
"
set"
;
foreach
(
$row
as
$key
=>
$val
)
{
$sql_row
.=
"
"
.
$key
.
"='"
.
$val
.
"',"
;
}
return
trim(
$sql_row
,
","
);
}
private
function
sqlExec(
$sql
)
{
try
{
$result
=
$this
->PDO->
exec
(
$sql
);
}
catch
(PDOException
$e
)
{
$this
->Msg(
$e
->getMessage());
}
return
$result
;
}
public
function
lastinsertid()
{
return
$this
->PDO->lastinsertid();
}
public
function
select(
$table
,
$fields
=
""
,
$where
=
""
,
$orderby
=
""
,
$sort
=
""
,
$limit
=
""
)
{
$fields
=
empty
(
$fields
)
?
"*"
:
$fields
;
$sqlSelect
=
$this
->sqlCreateSelect(
$table
,
$fields
,
$where
,
$orderby
,
$sort
,
$limit
);
$this
->query(
$sqlSelect
,
$where
);
}
private
function
sqlCreateSelect(
$table
,
$fields
=
"*"
,
$where
=
""
,
$orderby
=
""
,
$sort
=
""
,
$limit
=
""
)
{
$whereSql
=
empty
(
$where
)?
"
1=1 "
:
$this
->whereCreate(
$where
);
$orderbySql
=
empty
(
$orderby
)?
""
:
"
order by "
.
$orderby
.
"
"
.
$sort
;
$limitSql
=
empty
(
$limit
)?
""
:
"
limit "
.
$limit
;
$sql
=
"select
$fields from $table where "
.
$whereSql
.
$orderbySql
.
$limitSql
;
return
$sql
;
}
private
function
whereCreate(
$where
)
{
$whereSql
=
""
;
foreach
(
$where
as
$key
=>
$val
)
{
$whereSql
.=
"
"
.
$key
.
"=:"
.
$key
.
"
and"
;
}
return
$whereSql
.
"
1=1 "
;
}
private
function
query(
$sql
,
$where
)
{
try
{
$this
->prepare
=
$this
->PDO->prepare(
$sql
);
}
catch
(PDOException
$e
)
{
$this
->Msg(
"预处理sql出错信息:"
.
$e
->getMessage().
"<br>sql:("
.
$sql
.
")"
);
}
empty
(
$where
)?
""
:
$this
->bind(
$where
);
$this
->prepare
->execute();
}
private
function
bind(
$where
)
{
foreach
(
$where
as
$key
=>
$val
)
{
$this
->prepare->bindValue(
":"
.
$key
,
$val
);
}
}
public
function
selectOne()
{
$result
=
$this
->prepare->fetch(PDO::FETCH_ASSOC);
return
$result
;
}
public
function
selectAll()
{
$result
=
$this
->prepare->fetchAll(PDO::FETCH_ASSOC);
return
$result
;
}
public
function
selectCount()
{
$total
=
$this
->prepare->rowCount();
return
$total
;
}
}