⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.php

📁 PHP4_0入门与提高源程序代码
💻 PHP
字号:
<?

//对MySQL数据库进行操作的类
class MySQL_class {

//变量声明,保存着MySQL数据库的节点名、用户名、密码,所选择的数据库等参数
    var $db, $id, $result, $rows, $data, $a_rows;	
    var $user, $pass, $host;
/************************
function:	确定用户名和密码
$param:	$user	用户名
$param:	$pass	密码
*************************/
    function Setup ($user, $pass) {
        $this->user = $user;
        $this->pass = $pass;
    }

/************************
function:	选择数据库
$param:	$db	数据库名称
************************/
    function Create ($db) {
        if (!$this->user) {
            $this->user = "USERNAME"; //登陆的用户名 
        }
        if (!$this->pass) {
            $this->pass = "PASSWORD"; //登陆密码
        }
        $this->db = $db;
        $this->id = @mysql_pconnect($this->host, $this->user, $this->pass) or
            MySQL_ErrorMsg("Unable to connect to MySQL server: $this->host : '$SERVER_NAME'");		//建立与MySQL服务器的永久性连接
        $this->selectdb($db);	//选择数据库
    }

/*************************
$function:	选择数据库
$param:	$db	数据库名称
**************************/
    function SelectDB ($db) {
        @mysql_select_db($db, $this->id) or	//选择数据库
            MySQL_ErrorMsg ("Unable to select database: $db");
    }

/******************************
$function:	返回多行结果的查询
$param:	$query	查询的SQL语句
******************************/
    function Query ($query) {
        $this->result = @mysql_query($query, $this->id) or	//执行查询操作
            MySQL_ErrorMsg ("Unable to perform query: $query");
        $this->rows = @mysql_num_rows($this->result);	//返回结果包含的行数
        $this->a_rows = @mysql_affected_rows($this->result);	//操作所影响的行数
    }

/******************************
$function:	返回一个字段的查询
$param:	$query	查询的SQL语句
******************************/
    function QueryItem ($query) {
        $this->result = @mysql_query($query, $this->id) or
            MySQL_ErrorMsg ("Unable to perform query: $query");
        $this->rows = @mysql_num_rows($this->result);
        $this->a_rows = @mysql_affected_rows($this->result);
       $this->data = @mysql_fetch_array($this->result) or		//把结果保存到数组
            MySQL_ErrorMsg ("Unable to fetch data from query: $query");
        return($this->data[0]);
    }

/******************************
$function:	返回一行的查询
$param:	$query	查询的SQL语句
******************************/
	function QueryRow ($query) {
        $this->result = @mysql_query($query, $this->id) or
            MySQL_ErrorMsg ("Unable to perform query: $query");
        $this->rows = @mysql_num_rows($this->result);
        $this->a_rows = @mysql_affected_rows($this->result);
        $this->data = @mysql_fetch_array($this->result) or
            MySQL_ErrorMsg ("Unable to fetch data from query: $query");
        return($this->data);
    }

/********************************
function:	把结果保存到一个数组中
$param:	$row	行索引
*********************************/
    function Fetch ($row) {
        @mysql_data_seek($this->result, $row) or	//从查询结果中取回一行的内容
            MySQL_ErrorMsg ("Unable to seek data row: $row");
        $this->data = @mysql_fetch_array($this->result) or
            MySQL_ErrorMsg ("Unable to fetch row: $row");
    }

/*********************************
function:	向数据库中插入一行数据
	$param:	$query	插入的SQL语句
	**********************************/
    function Insert ($query) {
        $this->result = @mysql_query($query, $this->id) or
            MySQL_ErrorMsg ("Unable to perform insert: $query");
        $this->a_rows = @mysql_affected_rows($this->result);
    }

/**********************************
function:	更新数据库的内容
$param:	$query	更新的SQL语句
**********************************/
    function Update ($query) {
        $this->result = @mysql_query($query, $this->id) or
            MySQL_ErrorMsg ("Unable to perform update: $query");
        $this->a_rows = @mysql_affected_rows($this->result);
    }

/***********************************
function:	删除数据库中的内容
$param:	$query	删除的SQL语句
***********************************/
    function Delete ($query) {
        $this->result = @mysql_query($query, $this->id) or
            MySQL_ErrorMsg ("Unable to perform Delete: $query");
        $this->a_rows = @mysql_affected_rows($this->result);
    }
}

/******************************
function:	输出错误信息
$function		$msg	错误信息
******************************/
function MySQL_ErrorMsg ($msg) {
    echo("</ul></dl></ol>\n");
    echo("</table></script>\n");
    //显示错误信息
    $text  = "<font color=\"#ff0000\" size=+2><p>Error: $msg :";
    $text .= mysql_error();
    $text .= "</font>\n";
    die($text);
}
?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -