1

I'm trying emerging them(php mysql connection) to avoid from redundancy. But the major problem here is that whenever using "mysql_real_esacpe_string" it needs mysql_connect on top which might lead to an failure to emerge them in a function?

Example:

    function runquery($query){

        connect(); //Connecting database

        issue_query($query); //Calling mysql_query function

        disconnect($link); //Calling mysql_close function
    }
hakre
  • 193,403
  • 52
  • 435
  • 836
Eric T
  • 1,026
  • 3
  • 20
  • 42

2 Answers2

5

You should not open and close the connection for every query. Unless you do some long processing after you're done talking to the database, you don't need to close the connection at all; it'll get closed when execution ends and the resources are freed. Each reconnect involves significant overhead in establishing the TCP connection and performing the handshake and authentication before you can issue a query.

Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
  • You don't want to close your connection, as it will automatically be closed when your PHP script ends. Unless you expect your script to take a very long time, don't bother closing your database connections. – Pelle Aug 03 '11 at 08:56
  • @Pelle Why are you repeating what I said? – Dan Grossman Aug 03 '11 at 08:57
  • Whoo! I must have skipped a line while reading your answer. Aargh! Credits go to you. – Pelle Aug 03 '11 at 08:58
  • I thought it might have ended up reserving some resources if we don't close the connection? What about the case for a simple registration form? – Eric T Aug 03 '11 at 09:05
  • All resources are freed at end of execution. The connection is closed automatically. You do not need to close it. – Dan Grossman Aug 03 '11 at 09:08
  • Somewhere, Connection closes automatic after 3600 Seconds(in my server), mean 1 hour. So, It sometimes stops mysql connection because of more queries and so I get "User [my_user] already has more than 'max_user_connections' active connections" from my code.. so its still I need to do close by my self.. Here is similar question.http://stackoverflow.com/questions/4284194/terminating-idle-mysql-connections – Dharmik May 27 '14 at 13:12
2

You can also make your class which handles mysql connection to singleton.

class mysqlController {
private $connection;
private $db;
private static $instance;

private function __construct() {

}
public static function getInstance() {
    if(!self::$instance) { // First time this method is called
        self::$instance = new mysqlController();
    }

    return self::$instance;
    }
public function openConnection($db_host, $db_user, $db_password, $db_name)
{
    if(!$this->connection)
    {
        $this->connection = mysql_connect($db_host, $db_user, $db_password);

        if(!$this->connection)
        {
            die('Database error: ' . mysql_error());
        }
        else
        {
            $this->db = mysql_select_db($db_name, $this->connection);
            if(!$this->db)
            {
                die("Database error: " . mysql_error());
            }
        }
    }
}
}

To use the mysql-connection get the instace with getInstance()-function.

$connection = mysqlController::getInstance();
$connection->openConnection('host', 'user', 'pass', 'database');
$connection->query(.....); // For example

Of course you should also need to create here query function etc.

Waltsu
  • 612
  • 6
  • 10