Location: PHPKode > scripts > Connect class > connect-class/db_connect.php
                        class name:mysql
                        description:database abstraction class
                        purpose:to connect to mysql database and simplify sql queries
                        author name: ashwin morey
                        created on: 20/10/2005
                        modified by:
                        modified on:
                        methods used:database(constructor),connect(),query(),num_rows(),movenext(),getfield, getinsertID
    class database{
        //stores hostname or the server name
        var $hostName;
        //username for the database
        var $userName;
        //password for the database
        var $password;
        //database name
        var $dbName;
        //stores connection id
        var $conn;
        //stores the resource ID for the sql query
        var $rs;
        //stores properties of the record corresponding to that column
        var $record;
        //returns number of records returned by the sql query
        var $num;
        //returns the id of the latest inserted record
        var $latestID;
        function name:mysql(constructor)
        purpose:to set values for the variables, calls up the connect method()
        arguments:hostName, dbName, $userName, $password
        function database(){
            //parse the ini file here and assign values to the variables
            $ini = parse_ini_file("web.ini");
            while(list($key,$value) = each($ini)){
                if($key == 'hostName'){
                    $hostName = $value;
                else if($key == 'userName'){
                    $userName = $value;
                else if($key == 'password'){
                    $password = $value;
                else if($key == 'dbName'){
                    $dbName = $value;
            $this->hostName = $hostName;
            $this->userName = $userName;
            $this->password = $password;
            $this->dbName = $dbName;
        function name:connect()
        description:open a connection to mysql
        purpose:to open a connection to mysql database and select the database
        returns:connection ID
        function connect(){
            $this->conn = @mysql_pconnect($this->hostName,$this->username,$this->password)
            or die("Connection to the server failed");
            //select the database
            or die("No such database exist");
            return $this->conn;

        function name:query()
        description:pass sql query to the database
        purpose:to execute sql query
        arguments:$sql(sql query)
        returns:resource ID
        function query($sql){
            $this->rs = @mysql_query($sql,$this->conn)
            or die("There is error in the sql query");
            return $this->rs;

        function name:num_rows
        description:gives numbers of rows
        purpose:gives number of rows returned by sql query
        returns:number of rows
        function num_rows(){
            $num = @mysql_num_rows($this->rs);
            return $num;

        function name:movenext()
        description:navigate the database
        purpose:to fetch an object with properties that correspond to the fetched row
        returns:returns an object with properties of the fetched row
        function movenext(){
            $this->record = @mysql_fetch_object($this->rs);
            return $this->record;
        function name:getfield()
        description:retrieves the field value
        purpose:to get the field value corresponding to that record
        returns:field value corresponding to that record
        function getfield($field){
            return $this->record->$field;

        function name:getinsertID()
        description:retrieves the latest ID
        purpose:to get the ID of the latest inserted record
        (works only with auto increment fields of the database)
        returns:ID of the latest inserted record
        function getinsertID(){
          $this->latestID = @mysql_insert_id($this->conn);
          return $this->latestID;
        function name:close()
        description:close the connection
        purpose:to close connection with mysql database
        returns:true or false (boolean value)
        function close(){
          return @mysql_close($this->conn);

Return current item: Connect class