作者在 2011-10-03 10:50:23 发布以下内容
<?php
/*
创建自己的mysql_class类
*/
class mysql{
private $host;//为了数据库安全,设置私有属性
private $name;
private $pass;
private $bbs;
private $gb;
function __construct($host,$name,$pass,$bbs,$gb){
$this->host=$host;
$this->name=$name;
$this->pass=$pass;
$this->bbs=$bbs;
$this->gb=$gb;
$this->connect();
}
function connect(){ //连接数据库
$link=mysql_connect($this->host,$this->name,$this->pass);//连接数据库
mysql_select_db($this->bbs,$link);//选择数据库
mysql_query("SET NAMES '$this->gb'");//设置字符集
}
function query($link, $type = '') {
if(!($query = mysql_query($link))) $this->show('Say:', $link);
return $query;
}
//测试一下我的类是否成功,我就用个插入语句
function insert($db1,$name,$value){
$this->query("insert into $db1 ($name) value ($value)");
}
}
$db = new mysql('localhost','root','','bbs','gbk');//实例化
$db->insert('message','id,user,title,content,lastdate',"'','王志强','计算机','计算机',now()");
?>
其实,希望大家有更多的类,我们一起交流!
/*
创建自己的mysql_class类
*/
class mysql{
private $host;//为了数据库安全,设置私有属性
private $name;
private $pass;
private $bbs;
private $gb;
function __construct($host,$name,$pass,$bbs,$gb){
$this->host=$host;
$this->name=$name;
$this->pass=$pass;
$this->bbs=$bbs;
$this->gb=$gb;
$this->connect();
}
function connect(){ //连接数据库
$link=mysql_connect($this->host,$this->name,$this->pass);//连接数据库
mysql_select_db($this->bbs,$link);//选择数据库
mysql_query("SET NAMES '$this->gb'");//设置字符集
}
function query($link, $type = '') {
if(!($query = mysql_query($link))) $this->show('Say:', $link);
return $query;
}
//测试一下我的类是否成功,我就用个插入语句
function insert($db1,$name,$value){
$this->query("insert into $db1 ($name) value ($value)");
}
}
$db = new mysql('localhost','root','','bbs','gbk');//实例化
$db->insert('message','id,user,title,content,lastdate',"'','王志强','计算机','计算机',now()");
?>