Creating a session variable for DB connection

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
rpidcock
Forum Newbie
Posts: 4
Joined: Wed Mar 25, 2009 1:08 am

Creating a session variable for DB connection

Post by rpidcock »

I'm working on the development of a PHP app that will read/write to a database. Can I create a session variable that will hold the db connection information so I can reference that variable each time I make a call to the database rather than creating a connection. Also I will want to log users in based on user info contained within the db (username/pw).

Suggestions welcomed.
inquisitivedzign
Forum Newbie
Posts: 4
Joined: Sat Mar 28, 2009 9:51 pm

Re: Creating a session variable for DB connection

Post by inquisitivedzign »

use a class! set the top variables in a folder called globals.php, and the rest of the stuff below in mysql.class.php...then in a universal header or on the top of each page require_once(dir/filename);

hope this helps

Code: Select all

<?php
$host = 'localhost';
$user = 'username';
$pass = 'password';
$name = 'databaseName';
$db = &new MySQL($host,$user,$pass,$name);
?>
 
<?php
/**
* MySQL Database Connection Class
* @access public
* @package SPLIB
*/
class MySQL {
    /**
    * MySQL server hostname
    * @access private
    * @var string
    */
    var $host;
 
    /**
    * MySQL username
    * @access private
    * @var string
    */
    var $dbUser;
 
    /**
    * MySQL user's password
    * @access private
    * @var string
    */
    var $dbPass;
 
    /**
    * Name of database to use
    * @access private
    * @var string
    */
    var $dbName;
 
    /**
    * MySQL Resource link identifier stored here
    * @access private
    * @var string
    */
    var $dbConn;
 
    /**
    * Stores error messages for connection errors
    * @access private
    * @var string
    */
    var $connectError;
 
    /**
    * MySQL constructor
    * @param string host (MySQL server hostname)
    * @param string dbUser (MySQL User Name)
    * @param string dbPass (MySQL User Password)
    * @param string dbName (Database to select)
    * @access public
    */
    function MySQL ($host,$dbUser,$dbPass,$dbName) {
        $this->host=$host;
        $this->dbUser=$dbUser;
        $this->dbPass=$dbPass;
        $this->dbName=$dbName;
        $this->connectToDb();
    }
 
    /**
    * Establishes connection to MySQL and selects a database
    * @return void
    * @access private
    */
    function connectToDb () {
        // Make connection 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;
        }
    }
 
    /**
    * Checks for MySQL errors
    * @return boolean
    * @access public
    */
    function isError () {
        if ( $this->connectError )
            return true;
        $error=mysql_error ($this->dbConn);
        if ( empty ($error) )
            return false;
        else
            return true;
    }
 
    /**
    * Returns an instance of MySQLResult to fetch rows with
    * @param $sql string the database query to run
    * @return MySQLResult
    * @access public
    */
    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);
    }
}
 
/**
* MySQLResult Data Fetching Class
* @access public
* @package SPLIB
*/
class MySQLResult {
    /**
    * Instance of MySQL providing database connection
    * @access private
    * @var MySQL
    */
    var $mysql;
 
    /**
    * Query resource
    * @access private
    * @var resource
    */
    var $query;
 
    /**
    * MySQLResult constructor
    * @param object mysql   (instance of MySQL class)
    * @param resource query (MySQL query resource)
    * @access public
    */
    function MySQLResult(& $mysql,$query) {
        $this->mysql=& $mysql;
        $this->query=$query;
    }
 
    /**
    * Fetches a row from the result
    * @return array
    * @access public
    */
    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;
        }
    }
 
    /**
    * Returns the number of rows selected
    * @return int
    * @access public
    */
    function size () {
        return mysql_num_rows($this->query);
    }
 
    /**
    * Returns the ID of the last row inserted
    * @return int
    * @access public
    */
    function insertID () {
        return mysql_insert_id($this->mysql->dbConn);
    }
    
    /**
    * Checks for MySQL errors
    * @return boolean
    * @access public
    */
    function isError () {
        return $this->mysql->isError();
    }
}
?>

Code: Select all

 
Post Reply