1

I have a separate config file to set db parameters.

<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "aa");
?>

And when user entered the user name and password, I open the database connection and validate the user in a another php file.

<?php
include("config.php");
include("database.php");
$dbo = database::getInstance();
$result=$dbo->connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
if($result){
$un = $_POST['username'];
$pw=$_POST['password'];
$dbo->validte_User_Login($un,$pw);
}
else
print "false";?>

Next I want to do some manipulations in database in another php file. Here how can I geet the already open database connection again without including the config file and calling connect method by passing parameters again?

My database connection function is as below.

function connect($host, $user, $pass, $dbName)
 {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->dbName = $dbName;

$db_handle = mysql_connect($this->host, $this->user,$this->pass);
$db_found = mysql_select_db($this->dbName, $db_handle);

if ($db_found) {
print "Database Found<br/>";
return true;                                 
}
else
print "Database  not Found<br/>";
return false;
}
3
  • Return the $db_handle inside your connect() function. For other functions that require a database handle, you can call the connect() function inside it. Commented Feb 23, 2014 at 13:40
  • 2
    Can't you work with database::getInstance()? Commented Feb 23, 2014 at 13:41
  • I have used database::getInstance(). Then it gives INSERT command denied to user ''@'localhost' for table 'user_login' error. only Once I open the connection it gives the results. Commented Feb 23, 2014 at 13:55

1 Answer 1

1

I think there are currently 2 flaws in your code:

  1. Don't use mysql_* functions in new code - they are deprecated. See this answer for more information.

  2. You have a mix of functional and object oriented code. When working with the database, calling ::getInstance() implies that I get a working instance - how should an outstander know, if it was already connected or not and more importantly why should he care.

Both can be solved quite quickly, lets take a look at this getInstance() method:

class database {

    private $instance = NULL;

    public static function getInstance() {
        if (self::$instance === NULL) {
            include 'config.php';
            self::$instance = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        }

        return self::$instance;
    }

    //etc.

}

Now you can have code like this anywhere in your code

$db = database::getInstance();
$result = $db->query("...");
while ($row = $result->fetch_array()) {

}

because the instance will already be connected and if you have multiple calls to getInstance() it will connect only once.

Of course this shows only a snippet which can be improved at some points, e.g. when loading the db data or handling errors.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.