phpwind中的数据库操作类

yipeiwu_com6年前PHP代码库

<?php
/*来源:phpwind.net*/

Class DB {
var $query_num = 0;

function DB($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0) {
$this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
}
function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0) {
$pconnect==0 ? @mysql_connect($dbhost, $dbuser, $dbpw) : @mysql_pconnect($dbhost, $dbuser, $dbpw);
mysql_errno()!=0 && $this->halt("Connect($pconnect) to MySQL failed");
if($this->server_info() > '4.1' && $GLOBALS['charset']){
mysql_query("SET NAMES '".$GLOBALS['charset']."'");
}
if($this->server_info() > '5.0'){
mysql_query("SET sql_mode=''");
}
if($dbname) {
if (!@mysql_select_db($dbname)){
$this->halt('Cannot use database');
}
}
}
function close() {
return mysql_close();
}
function select_db($dbname){
if (!@mysql_select_db($dbname)){
$this->halt('Cannot use database');
}
}
function server_info(){
return mysql_get_server_info();
}
function query($SQL,$method='') {
$GLOBALS['PW']=='pw_' or $SQL=str_replace('pw_',$GLOBALS['PW'],$SQL);
if($method=='U_B' && function_exists('mysql_unbuffered_query')){
$query = mysql_unbuffered_query($SQL);
}else{
$query = mysql_query($SQL);
}
$this->query_num++;

//echo $SQL.'<br>'.$this->query_num.'<br>';
if (!$query)  $this->halt('Query Error: ' . $SQL);
return $query;
}

function get_one($SQL){

$query=$this->query($SQL,'U_B');

$rs =& mysql_fetch_array($query, MYSQL_ASSOC);

return $rs;
}

function pw_update($SQL_1,$SQL_2,$SQL_3){
$rt=$this->get_one($SQL_1);
if($rt){
$this->update($SQL_2);
} else{
$this->update($SQL_3);
}
}

function update($SQL) {
$GLOBALS['PW']=='pw_' or $SQL=str_replace('pw_',$GLOBALS['PW'],$SQL);
if($GLOBALS['db_lp']==1){
if(substr($SQL,0,7)=='REPLACE'){
$SQL=substr($SQL,0,7).' LOW_PRIORITY'.substr($SQL,7);
} else{
$SQL=substr($SQL,0,6).' LOW_PRIORITY'.substr($SQL,6);
}
}
if(function_exists('mysql_unbuffered_query')){
$query = mysql_unbuffered_query($SQL);
}else{
$query = mysql_query($SQL);
}
$this->query_num++;

//echo $SQL.'<br>'.$this->query_num.'<br>';

if (!$query)  $this->halt('Update Error: ' . $SQL);
return $query;
}

function fetch_array($query, $result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query, $result_type);
}

function affected_rows() {
return mysql_affected_rows();
}

function num_rows($query) {
$rows = mysql_num_rows($query);
return $rows;
}

function free_result($query) {
return mysql_free_result($query);
}

function insert_id() {
$id = mysql_insert_id();
return $id;
}

function halt($msg='') {
require_once(R_P.'require/db_mysql_error.php');
new DB_ERROR($msg);
}
}
?>

相关文章

PHP基于phpqrcode生成带LOGO图像的二维码实例

本文实例讲述了PHP基于phpqrcode生成带LOGO图像的二维码。分享给大家供大家参考。具体如下: 这里PHP使用phpqrcode生成带LOGO图像的二维码,使用起来很方便,代码中...

PHP版国家代码、缩写查询函数代码

复制代码 代码如下: <?php function transCountryCode($code) { $index=array('AA'=>'阿鲁巴', 'AD'=>...

生成ubuntu自动切换壁纸xml文件的php代码

复制代码 代码如下: <?php /* * 生成ubuntu自动切换壁纸xml文件 */ //图片目录 $dir = '/home/yuxing/background'; $hd...

php中HTTP_REFERER函数用法实例

本文实例分析了php中HTTP_REFERER函数用法。分享给大家供大家参考。具体分析如下: 利用php的http_referer函数来判断用户的来路,这是比较简单的,实例代码如下: 复...

php输出xml属性的方法

本文实例讲述了php输出xml属性的方法。分享给大家供大家参考。具体分析如下: 这段代码通过一个简单的范例演示了php如何读取xml文件并输出xml属性 <?php...