Page 1 of 1
How to connect to database by including a config file
Posted: Tue Nov 06, 2012 12:33 pm
by dharmeshb
I am very new to php and while I understood a basic CRUD tutorial done by NetBeans, I wanted to modify it a bit for my personal test CRUD project. I created a config.php file to store all my db information, but when I write my db class I am getting errors

Please advise.
config.php
Code: Select all
<?php
$myhost = 'localhost';
$myusername = 'username';
$mypassword = 'password';
$mydatabase = 'database';
?>
db.php
Code: Select all
<?php
class db extends mysqli{
include 'config.php';
public function __construct(){
parent::__construct(
$this->myhost,
$this->myusername,
$this->mypassword,
$this->mydatabase);
}
}
?>
Re: How to connect to database by including a config file
Posted: Tue Nov 06, 2012 12:46 pm
by requinix
You can't include files in a class definition. You can do it in a function (or a class's function) but not directly inside a class.
Re: How to connect to database by including a config file
Posted: Tue Nov 06, 2012 1:38 pm
by dharmeshb
Thanks requinix. When I include it outside, the database connection variables are not passed in the class.
What I want to do is have all my database connection info in one file and then use them to make connections as and when needed. This way I won't have to modify every file that uses the database variables.
How can I do that?
Re: How to connect to database by including a config file
Posted: Tue Nov 06, 2012 3:05 pm
by dharmeshb
Playing around I got it working but not to the point I need it..
How can I connect to database in user.php using db class?
I am getting this error
"Warning: mysqli::real_escape_string() [mysqli.real-escape-string]: Couldn't fetch User "
config.php
Code: Select all
<?php
$myhost = 'localhost';
$myusername = 'username';
$mypassword = 'password';
$mydatabase = 'database';
?>
db.php
Code: Select all
<?php
class db extends mysqli{
private $Host;
private $User;
private $Password;
private $Database;
public function __construct() {
include_once 'config.php';
$this->Host = $myhost;
$this->User = $myusername;
$this->Password = $mypassword;
$this->Database = $mydatabase;
if(mysqli_connect_error()){
echo "Cannot connect to database";
}
}
}
?>
user.php
Code: Select all
<?php
include_once 'db.php';
class User extends db{
public function checkUser($username,$password){
$username = $this->real_escape_string($username);
$password = $this->real_escape_string($password);
$result = mysqli_query("SELECT 1 from table");
return $result->data_seek(0);
}
}
?>
Re: How to connect to database by including a config file
Posted: Tue Nov 06, 2012 3:33 pm
by requinix
You need to call parent::__construct() like you had before - it's not done automatically.
Re: How to connect to database by including a config file
Posted: Tue Nov 06, 2012 9:59 pm
by dharmeshb
I think I am doing something wrong.. I added the parent construct like this
Code: Select all
<?php
include 'db.php';
class User extends db{
private $dbconn;
public function __construct() {
parent::__construct();
}
public function checkUser($username,$password){
$username = $this->dbconn->real_escape_string($username);
$password = $this->real_escape_string($password);
$result = $this->dbconn->query("SELECT 1 FROM table'");
return $result->data_seek(0);
}
}
?>
Then I call this class on my login page as
Code: Select all
$user = new \User;
$check = $user->checkUser($username, $password);
I get an error saying cannot call private db::__construct()
Re: How to connect to database by including a config file
Posted: Wed Nov 07, 2012 2:59 am
by requinix
This is at least the third iteration of code you've posted. They've all been different.
Stop writing code right now. Post your code. Don't make any changes to your code until you get a response about what is wrong. Then make those exact changes and nothing more, and post back with the results. Okay?
Re: How to connect to database by including a config file
Posted: Thu Nov 08, 2012 8:56 pm
by dharmeshb
Ooops!! Sorry for the mess I caused. My mind was too messed up with trying different things to make it work. I think I finally got it. Just need one clarification.
Is this possible?
I have my database connection info in config.php
I created a Database class in db.php by using the variables from config.php
Now this is what is confusing me..
Can I create another class called user in user.php, but use db.php to set my database connection?
I don't want to create database connection in every class.
No more code till I get this concept cleared
