if
(@preg_match("/[0-9]+(,[ ]?[0-9]+)?/is",
$limit
) && !preg_match("/ LIMIT [0-9]+(,[ ]?[0-
9]+)?$/is",
$sql
)) {
$sql
.= " LIMIT " .
$limit
;
}
return
$sql
;
}
protected
function
get_cache(
$sql
,
$method
) {
include_once
'./cache.php';
//若框架中使用__autoload(),这里可以不用加载文件
$cache
=
new
cache(
$this
->cache_dir,
$this
->cache_time);
$cache_file
= md5(
$sql
.
$method
);
$res
=
$cache
->get_cache(
$cache_file
);
if
(!
$res
) {
$res
=
$this
->
$method
(
$sql
);
$cache
->set_cache(
$cache_file
,
$res
);
}
return
$res
;
}
public
function
query_num() {
return
$this
->query_num;
}
public
function
set_cache_dir(
$cache_dir
) {
$this
->cache_dir =
$cache_dir
;
}
public
function
set_cache_time(
$cache_time
) {
$this
->cache_time =
$cache_time
;
}
public
function
query(
$sql
,
$limit
= null) {
$sql
=
$this
->getQuerySql(
$sql
,
$limit
);
$this
->sql =
$sql
;
$this
->rs =
$this
->mysqli->query(
$sql
);
if
(!
$this
->rs) {
echo
"<h2>".
$this
->mysqli->error."</h2>";
die
();
}
else
{
$this
->query_num++;
return
$this
->rs;
}
}
public
function
getOne(
$sql
) {
$this
->query(
$sql
, 1);
$this
->fetch_mode = MYSQLI_NUM;
$row
=
$this
->fetch();
$this
->free();
return
$row
[0];