-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
45 lines (43 loc) · 1.3 KB
/
db.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
* Class to initiate a new MySQL connection based on $dbInfo settings found in dbSettings.php
*
* @example
* $db = new database(); // Initiate a new database connection
* mysql_close($db->get_link());
*/
class database{
protected $databaseLink;
function __construct(){
include "dbSettings.php";
$this->database = $dbInfo['host'];
$this->mysql_user = $dbInfo['user'];
$this->mysql_pass = $dbInfo['pass'];
$this->openConnection();
return $this->get_link();
}
function openConnection(){
$this->databaseLink = mysql_connect($this->database, $this->mysql_user, $this->mysql_pass);
}
function get_link(){
return $this->databaseLink;
}
}
/**
* Insert an associative array into a MySQL database
*
* @example
* $data = array('field1' => 'data1', 'field2'=> 'data2');
* insertArr("databaseName.tableName", $data);
*/
function insertArr($tableName, $insData){
$db = new database();
$columns = implode(", ",array_keys($insData));
$escaped_values = array_map('mysql_real_escape_string', array_values($insData));
foreach ($escaped_values as $idx=>$data) $escaped_values[$idx] = "'".$data."'";
$values = implode(", ", $escaped_values);
$query = "INSERT INTO $tableName ($columns) VALUES ($values)";
mysql_query($query) or die(mysql_error());
mysql_close($db->get_link());
}
?>