Created
November 14, 2021 19:14
-
-
Save stevesohcot/bf78c009a928e8a07355a5f6ca13224f to your computer and use it in GitHub Desktop.
PHP Database Connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Database{ | |
| private $host; | |
| private $username; | |
| private $password; | |
| private $database; | |
| private $conn; | |
| public function __construct($host, $username, $password, $database){ | |
| $this->host = $host; | |
| $this->username = $username; | |
| $this->password = $password; | |
| $this->database = $database; | |
| $this->connect(); | |
| } | |
| public function connect(){ | |
| try{ | |
| $this->conn = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->database, $this->username, $this->password); | |
| } catch(PDOException $e){ | |
| print "Error!: " . $e->getMessage() . "<br/>"; | |
| die(); | |
| } | |
| } | |
| public function runSQL($query, $params=null){ | |
| try { | |
| $statement = $this->conn->prepare($query); | |
| $statement->execute($params); | |
| return true; | |
| } catch (PDOException $e) { | |
| print "Error" . $e->getMessage(); | |
| } | |
| } | |
| // more functions here | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment