1

i want to know if there is a way that always i have one opened connection into my database. i'm using this codes in my sites :

mysql class

class Mysql{
public static $Server   = "localhost";
static $User = 'root';
static $Password = '';
static $DataBaseName = 'teca.v1';
static $Connection;
static $DataBase;
static $LoadConnection = 0;
static $CharSet = "utf8";

static $TRANS =false;
static $TRSS  = array();

public function __construct(){
    if(Mysql::$LoadConnection==0){
        Mysql::$Connection = mysql_connect(Mysql::$Server,Mysql::$User,Mysql::$Password);
        Mysql::$DataBase   = mysql_select_db(Mysql::$DataBaseName,Mysql::$Connection);
        $charset=Mysql::$CharSet;
        mysql_query("SET NAMES $charset");
        Mysql::$LoadConnection=1;
    }
}
}

model class

class Model extends Mysql{
protected $datamembers = array();
protected $PrimaryKey="Id";
protected $TableName ="Table";
public $UnProtectedExecute = FALSE;

public  function ReadyString($value)
{
    if($this->UnProtectedExecute == TRUE)
        return addslashes($value);
    else
        return $value;
}
public function __set($variable, $value){
    $this->datamembers[strtolower($variable)] = $value;
}

public function __get($variable){
    return $this->datamembers[strtolower($variable)];
}

and one of my objects :

class LibraryFiles extends Model{
    public $TableName   = 'library_files';
}

but i dont know is this right and just this code create 1 connection for all of the object in my project?

tereško
  • 56,151
  • 24
  • 92
  • 147
Prof Nami
  • 97
  • 1
  • 11
  • yes , my objects are all inherit from model class and model from mysql – Prof Nami Mar 14 '13 at 05:48
  • Yes and because of that it calls its constructor method and creates mysql connection. – Yogesh Suthar Mar 14 '13 at 05:50
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – tereško Mar 14 '13 at 06:52

2 Answers2

1

What you are looking for a PHP Singleton Database Class. Use below links to get the idea. I guess I don't have to repeat all here.

http://www.matthewelliston.com/php-singleton-database-class/

Global or Singleton for database connection?

Creating the Singleton design pattern in PHP5

Community
  • 1
  • 1
Techie
  • 42,101
  • 38
  • 144
  • 232
  • Singletons are not a very good idea. They tend to tightly couple the code to the singleton making it harder to maintain as time passes. – Bart Mar 14 '13 at 05:52
0

Your design doesn't make very much sense. You should create separate the classes for MySQL and Model. The MySQL class should be instantiated once and be used throughout the whole script. So it should make only one connection. Then let the Model use the instance of the MySQL class to operate on the data.

Bart
  • 16,076
  • 5
  • 54
  • 79