How to connect to database by including a config file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dharmeshb
Forum Commoner
Posts: 36
Joined: Wed Dec 14, 2011 12:17 pm

How to connect to database by including a config file

Post 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);
       }
}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to connect to database by including a config file

Post 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.
dharmeshb
Forum Commoner
Posts: 36
Joined: Wed Dec 14, 2011 12:17 pm

Re: How to connect to database by including a config file

Post 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?
dharmeshb
Forum Commoner
Posts: 36
Joined: Wed Dec 14, 2011 12:17 pm

Re: How to connect to database by including a config file

Post 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);
        }
}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to connect to database by including a config file

Post by requinix »

You need to call parent::__construct() like you had before - it's not done automatically.
dharmeshb
Forum Commoner
Posts: 36
Joined: Wed Dec 14, 2011 12:17 pm

Re: How to connect to database by including a config file

Post 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()
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to connect to database by including a config file

Post 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?
dharmeshb
Forum Commoner
Posts: 36
Joined: Wed Dec 14, 2011 12:17 pm

Re: How to connect to database by including a config file

Post 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 :)
Post Reply