Do I need a mysql php connection in every function using the database?

I am creating a php restful API, and currently I have database connection information in each function.

//Connect To Database $hostname=host; $username=username; $password=password; $dbname=dbname; mysql_connect($hostname, $username, $password) OR DIE('Unable to connect to database! Please try again later.'); mysql_select_db($dbname); mysql_query($sqlApiAccess) or die('Error, insert query failed'); 

What is the best way to do this. Can I have one database connection for each php file? Or I need to do this for every function that uses a database.

+7
source share
5 answers

Create config.php and add the code:

config.php:

 $hostname=host; $username=username; $password=password; $dbname=dbname; mysql_connect($hostname, $username, $password) OR DIE('Unable to connect to database! Please try again later.'); mysql_select_db($dbname); 

Then, in any file you want to use mysql, add the following:

script2.php

 <?php require_once 'config.php'; mysql_query($sqlApiAccess) or die('Error, insert query failed'); ?> 
+12
source

To avoid creating a new database connection every time, we can use the Singleton design template -

we need to have a database class to handle the connection to the database -

Database.class.php

 <?php class Database { // Store the single instance of Database private static $m_pInstance; private $db_host='localhost'; private $db_user = 'root'; private $db_pass = ''; private $db_name = 'databasename'; // Private constructor to limit object instantiation to within the class private function __construct() { mysql_connect($this->db_host,$this->db_user,$this->db_pass); mysql_select_db($this->db_name); } // Getter method for creating/returning the single instance of this class public static function getInstance() { if (!self::$m_pInstance) { self::$m_pInstance = new Database(); } return self::$m_pInstance; } public function query($query) { return mysql_query($query); } } ?> 

& we can call it from other files -

other.php

 <?php include 'singleton.php'; $pDatabase = Database::getInstance(); $result = $pDatabase->query('...'); ?> 
+12
source

There is no need to establish a connection in each function. you need to create a connection file, for example conn.php , and execute the connection requests.

 <?php mysql_connect("localhost", "admin", "1admin") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); ?> 

in any other file where you want to connect to the database, just write this line

 <?php include("conn.php");?> 

In this file you can run any request.

+2
source

do the following:

 $db_connection= mysql_connect($hostname, $username, $password) OR DIE('Unable to connect to database! Please try again later.'); 

And every time you want to request:

 mysql_query("my_query",$db_connection); 

Please note: if you connect to the database in a function, you will need a global $db_connection .

And when you want to close the database connection:

 mysql_close($db_connection); 
+1
source

Why can't you move the connection information to the configuration and call mysql_connect to some factory?

eg.

 class ConnectionFactory { public static MysqlConnection CreateConnection() { $connection = mysql_connect(Config::$host, Config::$port etc); mysql_select_db($connection, Config::$schema); return $connection; } } 

and then in your code

 $connection = ConnectionFactory::CreateConnection(); mysql_query($connection, $sqlApiAccess) or die('Error, insert query failed'); 
0
source

All Articles