Jump to content

Jay88

Members
  • Posts

    16
  • Joined

  • Last visited

Jay88's Achievements

Member

Member (2/5)

0

Reputation

  1. The way i keep the code, this works class DB { protected static $con; public static function getConnection(){ try{ self::$con = new PDO( 'mysql: host= localhost; dbname=db1', 'root', 'password'); self::$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$con->setAttribute( PDO::ATTR_PERSISTENT, false ); } catch (PDOException $e) { echo "Could not connect to database."; exit; } //Returns Writeable db connection return self::$con; } } and I call it with this $con = DB::getConnection();
  2. so the difference betweent my code and his code Is that I had it as private function _construct() in my original code. is that wrong on my part ??? <?php class DB { protected static $con; private function _construct() { try{ self::$con = new PDO( 'mysql: host= localhost; dbname=db1', 'root', 'password'); self::$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$con->setAttribute( PDO::ATTR_PERSISTENT, false ); echo "Connected"; } catch (PDOException $e) { echo "Could not connect to database."; exit; } //Returns Writeable db connection return self::$con; } public static function getConnection(){ //If this instance was not beem started, start it. if(!self::$con){ new DB(); } //Returns Writeable db connection return self::$con; } } ?>
  3. My code... I think my issue is on the static public function and how is being called I am calling this from another file $con = DB::getConnection(); <?php class DB { protected static $con; public static function getConnection(){ try{ self::$con = new PDO( 'mysql: host= localhost; dbname=db1', 'root', 'password'); self::$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$con->setAttribute( PDO::ATTR_PERSISTENT, false ); echo "Connected"; } catch (PDOException $e) { echo "Could not connect to database."; exit; } //Returns Writeable db connection return self::$con; } public static function getConnection(){ //If this instance was not beem started, start it. if(!self::$con){ new DB(); } //Returns Writeable db connection return self::$con; } } ?>
  4. requinix, I did, but it didn't work FYI; I am running Xammp Apache and the latest version of PHP and MySQL
  5. barand I also tried this but didn't quiet work class DB { protected static $con; public static function getConnection(){ try{ self::$con = new PDO( 'mysql: host= localhost; dbname=db1', 'root', 'password'); self::$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$con->setAttribute( PDO::ATTR_PERSISTENT, false ); } catch (PDOException $e) { echo "Could not connect to database."; exit; } } public static function getConnection(){ //If this instance was not beem started, start it. if(!self::$con){ new DB(); } //Returns Writeable db connection return self::$con; } }
  6. I change it to this and I still don't get an error, any ideas? <?php class DB { protected static $con; private function _construct() { $servername = "localhost"; $database = "db1"; $username = "root"; $password = "password"; try { $con = new PDO("mysql:host=$servername;dbname=$database", $username, $password); // set the PDO error mode to exception $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } } public static function getConnection(){ //If this instance was not beem started, start it. if(!self::$con){ new DB(); } //Returns Writeable db connection return self::$con; } } ?>
  7. Here is my code, I am trying to create a function that connects to my local SQL DB using PDO instead of mysqli It looks like I can connect, the issue is that...I can't get my error message to show. "Could not connect to the database" will not come up, when I know that my user name and password are incorrect. I try changing the root name to test to get the error but it doesn't show up ....I can't see my mistake <?php //load test ...un comment exit test db con //exit ('test db con'); } class DB { protected static $con; private function _construct() { try{ self::$con = new PDO( 'mysql: host= localhost; dbname=testdb', 'root', 'password'); self::$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$con->setAttribute( PDO::ATTR_PERSISTENT, false ); } catch (PDOException $e) { echo "Could not connect to database."; exit; } } public static function getConnection(){ //If this instance was not beem started, start it. if(!self::$con){ new DB(); } //Returns Writeable db connection return self::$con; } } ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.