-1

Im trying to create my own working template with php functions set for all mysqli commands

now here is my config.php

<?php

/** Configuration Variables **/

define ('DEVELOPMENT_ENVIRONMENT',true);

if (DEVELOPMENT_ENVIRONMENT == true) {
define('DB_NAME', 'rdb');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_HOST', 'localhost');
} else {
 define('DB_NAME', 'db47');
define('DB_USER', 'dbo');
define('DB_PASSWORD', 'S');
define('DB_HOST', 'd.db.1and1.com');

and this is a page that im calling sql_functions.php

<?php
//create db connection function
function connect()
{
$connect = mysqli_connect(DB_HOST , DB_USER , DB_PASSWORD, DB_NAME) 
or die ("Failed to connect to MySQL: " . mysqli_connect_error()); 
}

//create db disconect function
function disconnect()
{
$close = mysqli_close();
}

connect();
disconnect();

?>

now ive already tested the connection function but the disconnect function keeps bringing up the following error Warning: mysql_close(): no MySQL-Link resource supplied in R:\UniServer\www\application\functions\sql_functions.php on line 12

i have tried calling the variable within the connect function so function disconnect(){ $close = mysqli_close($connect);}

but this still bring up errors . . .please help thanks in advance and im still learning so would rather have my mistake explained so that i may correct rather than the answer just written for me :)

Jason Demitri
  • 63
  • 1
  • 14

3 Answers3

1

The problem is variable scope. The disconnect() function has no reference to $connect.

I wouldn't recommend doing it this way. And while I also don't recommend a wrapper class, it is better than such functions. Instead consider a class, singleton, or global object shared via dependency injection.

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • thank you for taking the time to reply been reading everything in sight to do with singleton and class mainly `http://thanura.blogspot.co.uk/2010/04/php5-db-connection-class-with-mysqli.html`for singleton and http://www.daniweb.com/web-development/php/threads/190244/php-database-connection-class` for class and was wondering if there is any advice as to which is the best method for OOP and security there seems to be alot of debate into classes even read in some places that classes are NOT good practice along with using PDO instead of mysqli . . .any advice to be given ? – Jason Demitri Jul 09 '13 at 07:43
  • Personally, I avoid it all with a global DB object. I create the object in a config file and pass it around (dependency injection) as necessary. As far as PDO or mysqli, that's up to you. I use mysqli as it offers a few lower level methods that the PDO abstraction doesn't. Again, this is *how I do it* and I am by no means saying it is *the way to do it*. – Jason McCreary Jul 09 '13 at 14:43
0

change your disconnect() function to be like this :

function disconnect()
{
mysqli_close();
}

or

function disconnect()
{
mysqli_close($connect);
}

if you define a function into a variable, the function won't be executed. may it help :D

Oki Erie Rinaldi
  • 1,835
  • 1
  • 22
  • 31
-1

This is my current solution however final will be in mysqli

config.php

 <?php
/** Configuration Variables **/

define ('DEVELOPMENT_ENVIRONMENT',true);

class Dbconfig {
protected $host;
protected $username;
protected $password;
protected $database;

function Dbconfig(){
if (DEVELOPMENT_ENVIRONMENT == true) {
$this -> host =  'localhost';
$this -> username = 'root';
$this -> password = 'root';
$this -> database= 'db';

} else {
$this -> host =  '.db.1and1.com';
$this -> username = 'dbo';
$this -> password = 'SW';
$this -> database = 'db4';

}   
}

}
?>

connect.class.php

<?php

class createConnection  extends Dbconfig //create a class for make connection
{

function connectToDatabase() // create a function for connect database
{

    $conn= mysql_connect($this->host,$this->username,$this->password);

    if(!$conn)// testing the connection
    {
        die ("Cannot connect to the database");
    }

    else
    {

        $this->myconn = $conn;

        echo "Connection established";

    }

    return $this->myconn;

}

function selectDatabase() // selecting the database.
{
    mysql_select_db($this->database);  //use php inbuild functions for select database

    if(mysql_error()) // if error occured display the error message
    {

        echo "Cannot find the database ".$this->database;

    }
     echo "Database selected..";       
}

function closeConnection() // close the connection
{
    mysql_close($this->myconn);

    echo "Connection closed";
}

//now test the connection

} 
$connection = new createConnection(); //i created a new object

$connection->connectToDatabase(); // connected to the database

echo "<br />"; // putting a html break

$connection->selectDatabase();// closed connection

echo "<br />";

$connection->closeConnection();
?>

the theory behind it is that the db details are kept in config.php which is pulled in through bootstrap.php and then the classes are loaded with test attached. apart from changing to mysqli and removing the test can anyone see were i could improve this as in security or ease to use around a large scale site. Thank in advanced

Jason Demitri
  • 63
  • 1
  • 14