Skip to content

Instantly share code, notes, and snippets.

@serhanozcan
Last active June 6, 2018 21:44
Show Gist options
  • Save serhanozcan/0c79b08d8e603a8b99f4280d812804c1 to your computer and use it in GitHub Desktop.
Save serhanozcan/0c79b08d8e603a8b99f4280d812804c1 to your computer and use it in GitHub Desktop.
PDO CRUD Shortway Functions
<?php
//// Database connection
header('Content-Type: text/html; charset=utf-8');
$k_id = "username"; // database username
$k_pass = "password"; // database password
$k_host = "localhost"; // host
$k_db = "database"; // database name
try {
$db = new PDO("mysql:host={$k_host};dbname={$k_db};charset=utf8", $k_id, $k_pass);
// Default charset UTF-8
$db->exec("SET NAMES 'utf8';");
$db->exec("SET CHARSET 'utf8;'");
} catch ( PDOException $e ){
// die($e->getMessage()); // Catch errors.
die("DATABASE NOT FOUND!"); // This is the message!
}
/**********************************************************************/
/// Select (Fetch)
function getData($table, $selects, $row, $id)
{
global $db;
$data = $db->query("SELECT {$selects} FROM {$table} WHERE {$row} = '{$id}'")->fetch(PDO::FETCH_ASSOC);
return $data;
}
// Function usage
$singleselect = getData("users", "*", "id", "1");
/*********************************************************************/
//// Select All (Fetch All)
function allFetch($table, $selects, $manuel)
{
global $db;
$data = $db->query("SELECT {$alanlar} FROM {$tablo} {$manuel}", PDO::FETCH_ASSOC);
return $data;
}
// Function usage
$alltable = allFetch("users", "*", "ORDER BY id DESC");
/*********************************************************************/
///Delete Data
function delData($table, $row, $id)
{
global $db;
$query = $db->prepare("DELETE FROM {$table} WHERE {$row} = :id");
$delete = $query->execute(array(
'id' => $id
));
}
// Fonksiyon Kullanımı
$deleteuser = delData("users", "id", "1");
/*********************************************************************/
/// Insert Data
function insertData($rows, $values, $table)
{
global $db;
$other = array();
$query = "";
$count = count($values);
$a = 0;
for($i = 0; $i < $count; $i++)
{
$a++;
if($a == $count)
$query .= $rows[$i]." = ?";
else
$query .= $rows[$i]." = ?,";
}
$sql = $db->prepare("INSERT INTO {$table} SET {$query}");
$insert = $sql->execute($values);
if ( $insert )
return true;
else
return false;
}
// Function Usage
// There are 2 way for usage, This one,
$rows = array("username", "password");
$values = array("serhanozcan", "pass123");
$insertuser = insertData($rows, $values, "users");
// Second way
$insertuser = insertData(array("username", "password"), array("serhanozcan", "pass123"), "users");
/*********************************************************************/
////Update Data
function updateData($rows, $values, $table, $target, $id)
{
global $db;
$other = array();
$query = "";
$a = 0;
$count = count($values);
for($i = 0; $i < $count; $i++)
{
$a++;
if($a == $count)
$query .= $rows[$i]." = :a_".$rows[$i];
else
$query .= $rows[$i]." = :a_".$rows[$i].",";
}
$b = 0;
foreach( $rows as $rows )
{
$other["a_".$rows] = $values[$b];
$b++;
}
$other["id"] = $id;
$sql = $db->prepare("UPDATE {$table} SET {$query} WHERE {$target} = :id");
$update = $sql->execute($other);
if ( $update )
return true;
else
return false;
}
// Function usage
$rows = array("username", "password");
$values = array("serhanozcan", "pass123");
$updateuser = updateData($rows, $values, "users", "id", "1");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment