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; } } ?>
  8. Thanks that was a lot of help ....
  9. Noj SQl error and is on but i will check again , thx
  10. I posted my whole code ....Still didn't work
  11. no error i just don't get results
  12. Still didn't work .... here is the whole code is his .... $sql = "SELECT * FROM NBA2017 WHERE player='$player' ORDER BY `date` DESC LIMIT 10"; $result = $connect_NBAdata->query($sql); if ($result->num_rows > 0) { while($row =$result->fetch_assoc()){ $id = $row ["id"]; $player = $row ["player"]; $team = $row ["team"]; $matchup = $row ["Matchup"]; $date = $row ["date"]; $wl = $row ["W/L"]; $min = $row ["min"]; $pts = $row ["pts"]; $fgm = $row ["fgm"]; $fga = $row ["fga"]; $fgpct = $row ["fgpct"]; $pm3 = $row ["3pm"]; $pa3 = $row ["3pa"]; $ppct3 = $row ["3ppct"]; $ftm = $row ["ftm"]; $fta = $row ["fta"]; $ftpct = $row ["ftpct"]; $oreb = $row ["oreb"]; $dreb = $row ["dreb"]; $reb = $row ["reb"]; $ast = $row ["ast"]; $stl = $row ["stl"]; $blk = $row ["blk"]; $tov = $row ["tov"]; $pf = $row ["pf"]; $plusmin = $row["pm"]; $opp = $row ["opp"]; $ha = $row ["ha"]; $fpts = $row ["fpts"]; $fppmp = $row ["fppmp"]; $pos = $row ["pos"]; $ptype = $row ["ptype"]; $nonptsfpp = $row ["nonptsfpp"]; $ptsfppm = $row ["ptsfppm"]; $twopts = $row ["2pts"]; $threepts = $row ["3pts"]; $ft = $row ["ft"]; $util = $row ["util"]; $utilpmp = $row ["utilpmp"]; $caltot = $row ["caltot"]; $year = $row["year"]; $count = $row ["count"]; echo "<br>" . "Player: " . $player ." - ". $date . " - " . $team . " - " . $fppmp . " - " .$fpts ; } }else { echo "0 results"; }
  13. So i haven't coded in a while ... and can't figure out why is this happening If i did a Select statement like this ... $sql = "SELECT id, player, team, date, min, fppmp, fpts FROM NBA2017 WHERE player='$player' ORDER BY date DESC LIMIT 10 "; it works!! But i have 42 columns on the DB.... So I changed it to this .. $sql = "SELECT * FROM NBA2017 WHERE player='$player' ORDER BY date DESC LIMIT 10 "; and i get no results ... Maybe basic but i need help!!! Thanks in advance!!
  14. Can sugest a tutorial.. I been on the look but it seems I am doing something that is not very popular lol Thanks for the sugestion BTW
×
×
  • 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.