-3

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();
tereško
  • 56,151
  • 24
  • 92
  • 147
user3760639
  • 73
  • 1
  • 4
  • what about `$db = new DB(); $get = $db->getAll();` ?? you never instanciate the object...instead you are calling `getAll()` as a static method which is wrong – dev7 Sep 07 '14 at 18:12
  • you probably could adopt [this](http://stackoverflow.com/a/11369679/727208) approach, but .. emm .. what exactly is your question? – tereško Sep 07 '14 at 18:17

1 Answers1

-6

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

Daimos
  • 1,443
  • 9
  • 25