santosh22 Posted June 23, 2013 Share Posted June 23, 2013 Hi All, I have created a PHP Class file which is called from my php page. This class contains many functions to do the database operations. For the database operations, I have created a different class (db.inc.php) which is included in my calling page. however, for each of the functions that I call from the class to do a database operation, I need to include the db.inc.php class in order to execute the functions. Kindly help me in avoiding this. Calling PHP Page: include "inc/db.inc.php"; PHP Class Functions: function __construct() { include "db.inc.php"; $db = new mysqli_ext($mysql_hostname, $mysql_username, $mysql_password, $mysql_database); } public function request_app($app_id) { include "db.inc.php"; $arr_details; $db = new mysqli_ext($mysql_hostname, $mysql_username, $mysql_password, $mysql_database); $arr_details = $db->select("select a.*, b.APPL_NAME from tbldetails a, tbltype b where a.APPL_ID = b.APPL_ID and a.ID = ?", 's', $app_id); return $arr_app_details; } If I remove the include from the above function, it just does not work. Thanks. Quote Link to comment Share on other sites More sharing options...
Strider64 Posted June 23, 2013 Share Posted June 23, 2013 (edited) First, connecting to a database by using an include files is just silly when one can do something like this: <?php # PDO database: only one connection is allowed. class Database { private $_connection; // Store the single instance. private static $_instance; // Get an instance of the Database. // @return Database: public static function getInstance() { if (!self::$_instance) { self::$_instance = new self(); } return self::$_instance; } // Constructor - Build the PDO Connection: public function __construct() { $db_options = array( PDO::ATTR_EMULATE_PREPARES => false // important! use actual prepared statements (default: emulate prepared statements) , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION // throw exceptions on errors (default: stay silent) , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC // fetch associative arrays (default: mixed arrays) ); $this->_connection = new PDO('mysql:host=localhost;dbname=cms;charset=utf8', 'username', 'password', $db_options); } // Empty clone magic method to prevent duplication: private function __clone() {} // Get the PDO connection: public function getConnection() { return $this->_connection; } } You could do something like this for a mysqli type of connection: Then you can autoload your class(es) in an include file (utilities.inc.php for example) that you would put at the top of you php file (index.php for example). // Autoload classes from "classes" directory: function class_loader($class) { require('classes/' . $class . '.php'); } spl_autoload_register('class_loader'); I would suggest reading an book on PHP Object-Oriented Programming (a recent version). Oh, to connect you would do something like: // Connect to Database: $db = Database::getInstance(); // Actually it's an instance of a class, it's just worded badly. $pdo = $db->getConnection(); Edited June 23, 2013 by Strider64 Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted June 23, 2013 Solution Share Posted June 23, 2013 You don't need, and shouldn't, reconnect before each query. It's usually the slowest part of the process. You only to connect once per page. I'd connect to the database then pass $db to the class constructor. Inside the function you then reference $this->db class foo { private $db; function __construct($db) { $this->db = $db; } function bar() { $result = $this->db->query("SELECT blah from table"); .... return $blah; } } $db = new mysqli(......); $obj = new foo($db) Quote Link to comment Share on other sites More sharing options...
santosh22 Posted June 23, 2013 Author Share Posted June 23, 2013 Hi Barand, Thanks for your response. I tried your option & now getting the below error. Catchable fatal error: Object of class mysqli_ext could not be converted to string Is there a way to declare the $db as a database object? Thanks. Quote Link to comment Share on other sites More sharing options...
santosh22 Posted June 23, 2013 Author Share Posted June 23, 2013 Thanks Barand & Strider64. I have got a better approach for the code with your help. Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.