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 | |
class DataBaseManager | |
{ | |
public function GetData($query, $user, $pass) | |
{ | |
try { | |
$db_result = []; | |
$conn = new PDO("mysql:host=localhost;dbname=test", $user, $pass); | |
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$conn->exec("set names utf8"); | |
reset($query); | |
$db_name = key($query); | |
$conn->exec('USE ' . $db_name); | |
$db_result['r'] = $conn->query($query[$db_name], PDO::FETCH_ASSOC); | |
$count = $db_result['r']->rowCount(); | |
$db_result['c'] = $count; | |
if (0 == $count) { | |
$db_result['r'] = null; | |
} elseif (1 == $count) { | |
$db_result['r'] = $db_result['r']->fetch(); | |
} | |
return $db_result; | |
} catch (PDOException $e) { | |
echo $e->getMessage(); | |
return false; | |
} | |
} | |
public function DeleteData($query, $user, $pass) | |
{ | |
try { | |
$conn = new PDO("mysql:host=localhost;dbname=test", $user, $pass); | |
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$conn->beginTransaction(); | |
$conn->exec("set names utf8"); | |
foreach ($query as $db_name => $query_arr) { | |
$conn->exec('USE ' . $db_name); | |
foreach ($query_arr as $key => $query_string) { | |
$conn->exec($query_string); | |
++$ct; | |
} | |
} | |
$conn->commit(); | |
$conn = null; | |
return '<b>' . $ct . ' Records Deleted Successfully.</b>'; | |
} catch (PDOException $e) { | |
$conn->rollback(); | |
echo $e->getMessage(); | |
return false; | |
} | |
} | |
public function SetData($query, $user, $pass) | |
{ | |
try { | |
$db_result = []; | |
$conn = new PDO("mysql:host=localhost;dbname=test", $user, $pass); | |
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$conn->beginTransaction(); | |
$conn->exec("set names utf8"); | |
$count = ['u' => 0, 'i' => 0]; | |
foreach ($query as $db_name => $query_arr) { | |
$conn->exec('USE ' . $db_name); | |
foreach ($query_arr as $key => $query_string) { | |
$cq = $conn->exec($query_string); | |
if (strpos($query_string, 'UPDATE') !== false) { | |
$count['u'] += $cq; | |
} | |
if (strpos($query_string, 'INSERT') !== false) { | |
$count['i'] += $cq; | |
} | |
} | |
} | |
$conn->commit(); | |
$db_result['r'] = true; | |
$db_result['t'] = 'Updates: ' . $count['u'] . ', Inserts: ' . $count['i']; | |
return $db_result; | |
} catch (PDOException $e) { | |
$conn->rollback(); | |
echo $e->getMessage(); | |
return false; | |
} | |
} | |
} |
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
old method PDO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment