/
Connection.php
88 lines (72 loc) · 1.86 KB
/
Connection.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/*
* Copyright © 2009 Dylan Enloe
* ALL RIGHTS RESERVED
*
* This program is distributed under the terms of
* version 3 of the GNU Lesser General Public License.
* Please see the file LICENSE in this distribution
* for licensing information.
*
* Connection.php
* handles the connection to the mysql database as well as validation of
* input to avoid injection
* */
//a hack to make sure the settings stick...need to find out why I need
//to do this.
global $database_username, $database_password, $database_server,
$database_name;
require_once 'settings.php';
class Connection {
private $connection;
private $result;
function __construct()
{
global $database_username, $database_password, $database_server,
$database_name;
$this->connection = mysql_connect($database_server,
$database_username, $database_password)
or die ("could not connect");
mysql_select_db($database_name, $this->connection)
or die (mysql_error());
}
function __destruct()
{
// mysql_close($this->connection);
}
public function query($query)
{
//$query = $this->db_validate_string($query);
$this->result = mysql_query($query, $this->connection) or die("query error: " . mysql_error());
}
public function fetch_row()
{
$row = mysql_fetch_row($this->result);
return $row;
}
public function fetch_assoc()
{
$row = mysql_fetch_assoc($this->result);
return $row;
}
public function result_size()
{
return mysql_num_rows($this->result);
}
public function validate_string($string)
{
if (get_magic_quotes_gpc())
{
$string = stripslashes($string);
}
$string = strip_tags($string);
$string = mysql_real_escape_string($string);
return $string;
}
public function get_insert_ID()
{
$this->query("SELECT LAST_INSERT_ID();");
return $this->fetch_row();
}
}
?>