|
写了个类似DBHelepr
<?php
class DBHelper{
private $server="localhost:3306";//服务器
private $uid="root";//MySQL账号
private $pwd="sa";//MySQL密码
private $dataBaseName="db1";//数据库名称
private $conn;//链接对象
private $charSet="gb2312";//字符集类型
private $result;
public function __construct()
{
$this->Connection();
}
public function Free()
{
@ mysql_free_result($this->result);
}
public function __destruct() {
if(!empty($this->result))
{
$this->Free();
}
mysql_close($this->conn);
}
public function Connection()
{
$this->conn=mysql_connect($this->server,$this->uid,$this->pwd) or die('Could not connect: '.mysql_error());
if($this->conn)
{
mysql_query("SET NAMES $this->charSet");
mysql_select_db($this->dataBaseName,$this->conn);
}else
{
show_error("数据库连接错误","无法创建数据库连接");
}
}
/*数据库执行语句,可执行查询添加修改删除等任何sql语句*/
public function Query($sql) {
if ($sql == "") {
$this->show_error("SQL语句错误:", "SQL查询语句为空");
}
$this->sql = $sql;
$result = mysql_query($sql);
$this->result=$result;
/*if (!$result) {
//调试中使用,sql语句出错时会自动打印出来
if ($this->show_error) {
$this->show_error("错误SQL语句:", $sql);
}
} else {
$this->result = $result;
}*/
return $this->result;
}
public function QueryList($sql)
{
if ($sql == "") {
$this->show_error("SQL语句错误:", "SQL查询语句为空");
}
$resu=mysql_query($sql);
return mysql_fetch_array($resu);
}
public function QueryModel($sql)
{
if ($sql == "") {
$this->show_error("SQL语句错误:", "SQL查询语句为空");
}
$this->result=mysql_query($sql);
return mysql_fetch_object($this->result);
}
public function show_error($msg="",$code="")
{
echo "<span class='er'>错误信息:$msg</span><hr>";
echo "<span class='er'>错误语句:$code</span>";
}
}
?>
版权声明:本文为博主原创文章,未经博主允许不得转载。 |
|
|