mysql类
时间:2006-08-17 来源:tianwu
config.inc文件如下:
##############################
##############################
mysql.inc文件如下:
#############################
link_id = @mysql_pconnect($this->servername,$this->dbusername,$this->dbpassword)){
$this->halt("数据库服务器连接失败");
}
} else {
if (!$this->link_id = @mysql_connect($this->servername,$this->dbusername,$this->dbpassword)){
$this->halt("数据库服务器连接失败");
}
}
return $this->link_id;
}
function selectdb(){
if(!mysql_select_db($this->dbname,$this->link_id)){
$this->halt("数据库选择失败");
}
}
function query($query_string) {
$this->result = mysql_query($query_string, $this->link_id);
if (!$this->result) {
$this->halt("SQL 无效: " . $query_string);
}
return $this->result;
}
function fetch_array($queryid) {
$this->record = mysql_fetch_array($queryid);
if (empty($queryid)) {
$this->halt("Query id 无效:" . $queryid);
}
return $this->record;
}
function fetch_one_array($query) {
$this->result = $this->query($query);
$this->record = $this->fetch_array($this->result);
if (empty($query)) {
$this->halt("Query id 无效:" . $query);
}
return $this->record;
}
function halt($msg) {
$msg .= "请尝试刷新你的浏览器,如果仍然无法正常显示,请联系管理员.";
echo $msg;
exit;
}
function create_db($db)
{
if(!mysql_create_db($db,$this->link_id))
$this->halt("数据库创建失败");
}
function drop_db($db)
{
if(!mysql_drop_db($db,$this->link_id))
$this->halt("数据库删除失败");
}
}
$DB = new DB_MySQL;//定义一个对象
$DB->servername = $servername;
$DB->dbname = $dbname;
$DB->dbusername = $dbusername;
$DB->dbpassword = $dbpassword;
$DB->connect();
$DB->selectdb();
function close()
{
global $DB;
mysql_close($DB->link_id);
}
if (!$usepconnect) {
register_shutdown_function('close');// 在程序运行完毕时, 关闭数据库连接
}
?>
###############################
测试文件test.php如下:
###############################
$sql="SELECT *
FROM `tianwu_sort` ";
$result = $DB->query($sql);
while($array=$DB->fetch_array($result))
{
echo $array[title].'
';
}
$temp=$DB->fetch_one_array($sql);
echo $temp[title];
?>
###############################
##############################
##############################
mysql.inc文件如下:
#############################
link_id = @mysql_pconnect($this->servername,$this->dbusername,$this->dbpassword)){
$this->halt("数据库服务器连接失败");
}
} else {
if (!$this->link_id = @mysql_connect($this->servername,$this->dbusername,$this->dbpassword)){
$this->halt("数据库服务器连接失败");
}
}
return $this->link_id;
}
function selectdb(){
if(!mysql_select_db($this->dbname,$this->link_id)){
$this->halt("数据库选择失败");
}
}
function query($query_string) {
$this->result = mysql_query($query_string, $this->link_id);
if (!$this->result) {
$this->halt("SQL 无效: " . $query_string);
}
return $this->result;
}
function fetch_array($queryid) {
$this->record = mysql_fetch_array($queryid);
if (empty($queryid)) {
$this->halt("Query id 无效:" . $queryid);
}
return $this->record;
}
function fetch_one_array($query) {
$this->result = $this->query($query);
$this->record = $this->fetch_array($this->result);
if (empty($query)) {
$this->halt("Query id 无效:" . $query);
}
return $this->record;
}
function halt($msg) {
$msg .= "请尝试刷新你的浏览器,如果仍然无法正常显示,请联系管理员.";
echo $msg;
exit;
}
function create_db($db)
{
if(!mysql_create_db($db,$this->link_id))
$this->halt("数据库创建失败");
}
function drop_db($db)
{
if(!mysql_drop_db($db,$this->link_id))
$this->halt("数据库删除失败");
}
}
$DB = new DB_MySQL;//定义一个对象
$DB->servername = $servername;
$DB->dbname = $dbname;
$DB->dbusername = $dbusername;
$DB->dbpassword = $dbpassword;
$DB->connect();
$DB->selectdb();
function close()
{
global $DB;
mysql_close($DB->link_id);
}
if (!$usepconnect) {
register_shutdown_function('close');// 在程序运行完毕时, 关闭数据库连接
}
?>
###############################
测试文件test.php如下:
###############################
$sql="SELECT *
FROM `tianwu_sort` ";
$result = $DB->query($sql);
while($array=$DB->fetch_array($result))
{
echo $array[title].'
';
}
$temp=$DB->fetch_one_array($sql);
echo $temp[title];
?>
###############################
相关阅读 更多 +
排行榜 更多 +