3 class DatabaseConnection
{
5 private $sql_data = array();
6 protected $connection = null;
8 public $delete_status = false;
10 public $path_prefix="";
12 public $alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p","q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
14 function __construct($path_prefix = ""){
15 $this->path_prefix
= $path_prefix;
16 $sql_ini = fopen($path_prefix . "settings/sql.ini", "r");
17 if(!$sql_ini) $sql_ini = fopen($path_prefix . "$path_prefix/settings/sql.ini", "r");
18 while(!feof($sql_ini)){
19 $line = fgets($sql_ini);
20 $key = substr($line, 0, strpos($line, "="));
21 $value = trim(substr($line, strpos($line, "=")+
1));
22 $this->sql_data
[$key] = $value;
24 $this->connectToDatabase();
27 function connectToDatabase(){
29 $this->connection
= new PDO ("mysql:dbname=" . $this->sql_data
["database"] . ";host=" . $this->sql_data
["connection"],
30 $this->sql_data
["user"], $this->sql_data
["pass"]);
31 $this->connection
->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_EXCEPTION
);
33 } catch (PDOException
$e) {
34 $this->logsys
.= "Failed to get DB handle: " . $e->getMessage() . "\n";
38 function getConnection(){
39 return $this->connection
;
42 function addToTable($tablename, $paramaters){
43 $param_len = sizeof($paramaters);
47 foreach($paramaters as $key => $param){
49 $bind_string = ":$key";
50 $table_string = "`$key`";
54 $bind_string .= ",:$key";
55 $table_string .= ",`$key`";
58 $statement = $this->connection
->prepare("INSERT INTO `".$this->sql_data
["database"] ."`.`$tablename`($table_string) VALUES(" . $bind_string . ")");
61 foreach($paramaters as $key => $param){
62 $success = $statement->bindParam(":" . $key , $paramaters[$key]);
66 $statement->execute();
68 echo "<strong>" . $e->getMessage() . "</strong><br/>";
72 function deleteFromTable($table, $refining_paramater, $bind_val){
73 $statement = $this->connection
->prepare("DELETE FROM `$table` WHERE `$table`.`$refining_paramater` = :bindval");
74 $statement->bindParam(":bindval", $bind_val);
76 $response = $statement->execute();
78 echo "<strong>" . $e->getMessage() . "</strong><br/>";
82 function getPostDetails($table, $refining_paramater, $bind_val){
83 $statement = $this->connection
->prepare("SELECT * FROM `$table` WHERE `$table`.`$refining_paramater` = :bindval");
84 $statement->bindParam(":bindval", $bind_val);
86 $response = $statement->execute();
87 return $statement->fetchAll();
89 echo "<strong>" . $e->getMessage() . "</strong><br/>";
93 function updatePost($table, $table_search, $table_search_value, $keyed_params){
96 foreach($keyed_params as $key=>$value){
98 $set_string = "$key=:$key";
100 else $set_string .= ",$key=:$key";
103 $statement = $this->connection
->prepare("UPDATE `$table` SET $set_string WHERE $table_search=:param_to_find");
104 $statement->bindParam(":param_to_find", $table_search_value);
106 foreach($keyed_params as $key=>$value){
107 $statement->bindParam(":$key", $value);
111 $response = $statement->execute();
112 }catch(Exception
$e){
113 echo "<strong>" . $e->getMessage() . "</strong><br/>";