Hey all attempting to create a small MVC kind of application. Currently working on database class.
I am running a connection to the database every time i call a functions however failing to connect successfully and being able to send the $con variable to the other functions to do whatever with.
class DB {
function __construct() {
$con = mysqli_connect("localhost","c3337015","c3337015");
if (mysqli_connect_errno())
{
return false;
}else{
return $con;
}
}
function getAll(){
if(isset($con)){
echo "Connection Successful";
echo "Do MYSQLI stuff $con = new mysqli etc etc etc";
}else{
echo "Connection Failed";
echo "Leave me alone";
}
}
}
echo $get = DB::getAll();
First of all. See what is SINGLETON (it`s ideal thing for DB connection). Declare variable with connection and write singleton :) Example of singleton:
final class Singleton
{
/**
* class instance
*
* @var object
* @access private
*/
private static $oInstance = false;
private $_db;
/**
* get instance
*
* @return Singleton
* @access public
* @static
*/
public static function getInstance()
{
if( self::$oInstance == false )
{
self::$oInstance = new Singleton();
}
return self::$oInstance;
}
private function __construct() {}
}
So declare Your connection, declare method that will allow You to get connection and all done