working version
[Anonymous-Twitter-Board.git] / class / database-connection.php
blob1d38f22c09b75c1103a7c477486fb2007f600dd6
1 <?php
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(){
28 try {
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);
44 $bind_string = "";
45 $table_string= "";
46 $first_comma = false;
47 foreach($paramaters as $key => $param){
48 if(!$first_comma){
49 $bind_string = ":$key";
50 $table_string = "`$key`";
51 $first_comma = true;
53 else{
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 . ")");
60 $index = 0;
61 foreach($paramaters as $key => $param){
62 $success = $statement->bindParam(":" . $key , $paramaters[$key]);
63 $index++;
65 try{
66 $statement->execute();
67 }catch(Exception $e){
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);
75 try{
76 $response = $statement->execute();
77 }catch(Exception $e){
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);
85 try{
86 $response = $statement->execute();
87 return $statement->fetchAll();
88 }catch(Exception $e){
89 echo "<strong>" . $e->getMessage() . "</strong><br/>";
93 function updatePost($table, $table_search, $table_search_value, $keyed_params){
94 $first_entry = true;
95 $set_string = "";
96 foreach($keyed_params as $key=>$value){
97 if($first_entry){
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);
110 try{
111 $response = $statement->execute();
112 }catch(Exception $e){
113 echo "<strong>" . $e->getMessage() . "</strong><br/>";