Skip to content

Instantly share code, notes, and snippets.

@wyntonfranklin
Created June 12, 2019 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyntonfranklin/6d310d13b12c6364731fe5e77f28c4bf to your computer and use it in GitHub Desktop.
Save wyntonfranklin/6d310d13b12c6364731fe5e77f28c4bf to your computer and use it in GitHub Desktop.
Functions for your database operations
<?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