Skip to content

Instantly share code, notes, and snippets.

@stevesohcot
Created November 14, 2021 19:14
Show Gist options
  • Select an option

  • Save stevesohcot/bf78c009a928e8a07355a5f6ca13224f to your computer and use it in GitHub Desktop.

Select an option

Save stevesohcot/bf78c009a928e8a07355a5f6ca13224f to your computer and use it in GitHub Desktop.
PHP Database Connection
<?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