Copyright © 2005-2008 Andrea Tincani

AndreaPHP | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Problem with ImageCreateFromJPEG)Next Topic (Php ?) New Topic New Poll Post Reply
AndreaPHP Forum : PHP General : Problem with a SQL class...
Poster Message
neutrall
Level: Scholar


Registered: 28-03-2004
Posts: 43

icon Problem with a SQL class...

Hi, I got the followin class for MySQL that I got from a book :


class MySQL {

    var $host;         //Server Host Name
    var $dbUser;       //MySQL User Name
    var $dbPass;       //MySQL User Password
    var $dbName;       //Database To Use
    var $dbConn;       //Resource Link Identifier
    var $connectError; //Stores error messages

    /**
     * MySQL constructor
     */
    function MySQL($host, $dbUser, $dbPass, $dbName)
    {
        $this->host = $host;
        $this->dbUser = $dbUser;
        $this->dbPass = $dbPass;
        $this->dbName = $dbName;
        $this->connectToDb();
    }

    /**
     * Connection with MySql and Select a Database
     */
    function connectToDb()
    {
        //Connect to MySQL Server
        if (!$this->dbConn = @mysql_connect($this->host,$this->dbUser,$this->dbPass)) {
            trigger_error('Could not connect to server');
            $this->connectError = true;
        //Select Database
        } else if (!@mysql_select_db($this->dbName,$this->$dbConn)) {
            trigger_error('Could not select database.');
            $this->connectError = true;
        }
    }

    /**
     * Check for MySQL errors
     */
    function isError()
    {
        if($this->connectError) {
            return true;
        }
        $error = mysql_error($this->dbConn);
        if (empty($error)) {
            return false;
        } else {
            return true;
        }
    }
    
    /**
     * Return an instance of MySQLResult to fetch rows
     */
    function &query($sql)
    {
        if (!$queryResource = mysql_query($sql, $this->dbConn)) {
            trigger_error('Query failed: ' . mysql_error($this->dbConn) . ' SQL: ' . $sql);
        }
        return new MySQLResult($this, $queryResource);
    }
}




And This Class which return the result from the query :

/**
 * MySQLResult Data Fetching Class
 */
class MySQLResult {

    var $mysql;      //Instance of MySQL providing database connection
    var $query;      //Query Resource

    /**
     * MySQLResult constructor
     */
    function MySQLResult(&$mysql, $query)
    {
        this->mysql = &$mysql;
        this->query = $query;
    }

    /**
     *Fetches a row from the result
     */
    function fetch()
    {
        if($row = mysql_fetch_array($this->query, MYSQL_ASSOC)) {
            return $row;
        } else if ($this->size() > 0) {
            mysql_data_seek($this->query,0);
            return false;
        } else {
            return false;
        }
    }

    /**
     * Return the number of rows selected
     */
    function size()
    {
        return mysql_num_rows($this->query);
    }
    
    /**
     * Return the number Of Affected Row
     */
     function affected()
     {
        return mysql_affected_rows($this->mysql->dbConn);
     }
    
    /**
     * Return the Id ofthe last inserted row
     */
    function insertID()
    {
        return mysql_insert_id($this->mysql->dbConn)
    }

    /**
     * Check for MySQL errors
     */
    function isError()
    {
        return %this->mysql->isError();
    }
}
 



And I kept getting a error  on the line (this->mysql =&$mysql;) :


    function MySQLResult(&$mysql, $query)
    {
        this->mysql = &$mysql;
        this->query = $query;
    }



The Exact error message is : Parse error: syntax error, unexpected T_OBJECT_OPERATOR in J:\WEBSITE\WWW\class\mysql.php on line 119


can anyone help?

____________________________
A Stick give a wise man something to think about... and a fool, something to put in is mouth.

27-04-2005 at 07:27 PM
View Profile Send Email to User Show All Posts Visit Homepage ICQ | Quote Reply
neutrall
Level: Scholar


Registered: 28-03-2004
Posts: 43

icon Re: Problem with a SQL class...

I found the mistake... a realy stupid and classic one!!!


This is what I wrote :

   function MySQLResult(&$mysql, $query)
    {
        this->mysql = &$mysql;
        this->query = $query;
    }



and this is what I needed :


   function MySQLResult(&$mysql, $query)
    {
        $this->mysql = &$mysql;
        $this->query = $query;
    }




Notice the "$" in front of the this statement!!!

Problem solve!


____________________________
A Stick give a wise man something to think about... and a fool, something to put in is mouth.

28-04-2005 at 12:43 AM
View Profile Send Email to User Show All Posts Visit Homepage ICQ | Quote Reply
AndreaPHP Forum : PHP General : Problem with a SQL class...
Previous Topic (Problem with ImageCreateFromJPEG)Next Topic (Php ?) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: AndreaVB

Copyright © 2005-2008 Andrea Tincani

Powered by: tForum tForum Edition b0.92p1
Originally created by Toan Huynh (Copyright © 2000)
Enhanced by the tForumHacks team.