Chinaunix首页 | 论坛 | 博客
  • 博客访问: 242714
  • 博文数量: 76
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 745
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-28 16:04
文章分类

全部博文(76)

文章存档

2013年(2)

2010年(21)

2009年(53)

我的朋友

分类:

2010-08-03 23:50:46

<?php
class db{
    /** 数据库连接指针 */
    var $link_id;

    /** 返回记录条数 */
    var $num_rows;

    /** 命令请求指针 */
    var $query_id;

    /** 返回命令请求影响的列数 */
    var $affected_rows;

    /** Constructed function
     * @ Trait : create a connection for database */

    function __construct($rData)
    {
        $dbAddr = $rData[0];
        $dbBName = $rData[1];
        $dbBUser = $rData[2];
        $dbBPass = $rData[3];
        $this -> link_id = @mysql_pconnect($dbAddr, $dbBUser, $dbBPass) or die ($this-> link_id = "Mysql connection unsuccessful!");
        $selectDB = @mysql_select_db($dbBName, $this -> link_id) or die ($this-> link_id = "Database select unsuccessful!");
        mysql_query("set names 'utf8'");
        return $this -> link_id;
    }

    /** Close database connection
     * @ Parameter : $link_id - database link pointer
     * @ Trait : release database link pointer */

    function Quit($link_id){
        mysql_close($this->link_id);
    }

    /** Release pointer
     * @ Parameter : $query_id - pointer
     * @ Trait : release pointer */

    function free_result($query_id){
        @mysql_free_result($query_id);
    }

    /** execute SQL
     * @ Parameter : $sql_str - SQL语句
     * @ Trait : return a singleness Array */

    function Query($sql_str){
        $this->query_id = @mysql_query($sql_str, $this -> link_id);
        $this->num_rows = @mysql_num_rows($this->query_id);
        $RS=@mysql_fetch_array($this->query_id,MYSQL_ASSOC);
        if ($this->num_rows == 0) {
            $RS = FALSE;
        }
        $this->free_result($this->query_id);
        return $RS;
    }

    /** execute SQL
     * @ Parameter : $sql_str - SQL语句
     * @ Trait : return a multidimensional Array */

    function Query_array($sql_str){
        $this->query_id = @mysql_query($sql_str, $this -> link_id);
        $this->num_rows = @mysql_num_rows($this->query_id) ;
        if ($this->num_rows == 0) {
            $RSarray = FALSE;
            $this->free_result($this->query_id);
            return $RSarray;
        }else{
            for($i=0;$i<$this->num_rows;$i++){
                $RSarray[$i]=mysql_fetch_array($this->query_id,MYSQL_ASSOC);
            }
            $this->free_result($this->query_id);
            return $RSarray;
        }
    }

    /** execute SQL
     * @ Parameter : $sql_str - SQL语句
     * @ Trait : none data return */

    function Query_noreturn($sql_str){
        $this->query_id = mysql_query($sql_str, $this -> link_id);
        $this->affected_rows = @mysql_affected_rows();
        $this->num_rows = mysql_insert_id();
        if ($this->query_id){
            $RS = TRUE;
            return $RS;
        }else{
            $RS = FALSE;
            return $RS;
        }
    }

    function getQueryID()
    {
        return $this->num_rows;
    }
}
?>


mysql_query($sql_str, $this -> link_id)

新版本里,在链接操作中  增加了指定操作链的参数.
之前一直都只操作一个数据库,所以不需要第二个参数 也是无所谓的.(今天突然操作两个数据库,一直用的类文件失灵了. .找了很久才找到问题所在.)
说明:
不带参数,它使用你最近定义的连接。
带参数,它使用你的指定的连接。

  1. $link1 = @mysql_connect("192.168.0.1", "mysql_user", "mysql_password");
  2. $link2 = @mysql_connect("192.168.0.2", "mysql_user", "mysql_password");
若是两个链接,不带参数,程序将不知道你要操作哪个数据库.
阅读(904) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~