Created
June 12, 2019 19:35
-
-
Save wyntonfranklin/6d310d13b12c6364731fe5e77f28c4bf to your computer and use it in GitHub Desktop.
Functions for your database operations
This file contains 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 | |
function db_connect(){ | |
$host = "127.0.0.1"; | |
$user = ""; | |
$password = ""; | |
$db = "test"; | |
try { | |
$conn = new PDO("mysql:host=".$host.";dbname=".$db, $user, $password); | |
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
return $conn; | |
} | |
catch(PDOException $e) { | |
die($e->getMessage()); | |
} | |
} | |
function db_query( $sql, $params=array()){ | |
$conn = db_connect(); | |
$query = $conn->prepare($sql); | |
if(!empty($params)){ | |
$query->execute($params) or die ('Internal error'); | |
}else{ | |
$query->execute() or die ('Internal error'); | |
} | |
return $query; | |
} | |
function db_fetch_all( $sql, $params=array() ){ | |
$query = db_query($sql, $params); | |
try{ | |
$results = $query->fetchAll(PDO::FETCH_ASSOC); | |
}catch (Exception $e){ | |
return $e->getMessage(); | |
} | |
return $results; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment