i have a php class named as dbhandler and i want to include the php file dbcon in that class how can i do that plz help dbcon.php <?php $dbhost ="localhost"; $dbuser = "root"; $dbpass = "admin1234"; $dbname = "ue"; ?> dbhandler.php <?php ////////class defination//////////// class dbhandler { //include "dbcon.php"; public function connectdb() { //$connection=mysql_connect($dbhost,$dbuser,$dbpass) or print "not connected"; // mysql_select_db($dbname) or print "no DB"; $connection=mysql_connect("localhost","root" ,"admin1234") or print "not connected"; mysql_select_db("ue") or print "no DB"; } ///////function for closing the connection///////// public function disconnectdb() { mysql_close($connection); } ////////////function for adding the record to db //////////// public function addrecord($qry) { $results=mysql_query($qry) or die(mysql_error()); return true; } ////////////function for selecting the record from db //////////// public function selectrecord($qry) { $results=mysql_query($qry) or die(mysql_error()); return $results; } ////////////function for deleting the record from db //////////// public function deleterecord($qry) { $results=mysql_query($qry) or die(mysql_error()); return true; } /////////////function for updating record////////////// public function updaterecord($qry) { $results=mysql_query($qry) or die(mysql_error()); return true; } } ?>
Put it in the function connectdb() like this. public function connectdb() { include "dbcon.php"; PHP:
vbot, i think it is be more simple include "dbcon.php"; class dbhandler { //define data as global variable global $dbhost; global $dbuser; ...... public function connectdb() { and this variables will be can using along all script
ps/ your class bad for using. use simple function function q ($string) { return mysql_query($string) or $debugger .= (mysql_error()); } for debugging not using 'die'
It's better to use the define() function to define the details as globals. Once you do that you can just put: require_once("dbcon.php"); Code (markup): At the top of the code and then call the globals from within the class.
Actually, if you want to deal with classes, use __construct() ! Also, instead of using simple variables ( database information ), use define() function __construct() { require_once('dbcon.php'); // assign your config file values to class variables, etc. } PHP: