Skip to content

Commit 4a78e82

Browse files
author
peejeh
committed
Changed database connection function to take an array of information rather than specific details. This should make it easier to add additional database types.
1 parent b09b6a2 commit 4a78e82

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

mysql.php

+19-15
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ class mysql {
1414

1515
/**
1616
* Connect to the database.
17-
* @param str server
18-
* @param str username
19-
* @param str password
20-
*/
21-
function connect($server, $username, $password) {
22-
if ($this->db = @mysql_pconnect($server, $username, $password)) {
23-
return TRUE;
17+
* @param str[] config
18+
*/
19+
function connect($config) {
20+
if ($this->db = @mysql_pconnect(
21+
$config['server'],
22+
$config['username'],
23+
$config['password']
24+
)) {
25+
if ($this->select_db($config['database'])) {
26+
return TRUE;
27+
}
2428
}
2529
return FALSE;
2630
}
@@ -48,7 +52,7 @@ function select_db($database) {
4852
* @return resource A resultset resource
4953
*/
5054
function getColumns($table) {
51-
return mysql_query(sprintf('SHOW COLUMNS FROM %s', $table), $this->db);
55+
return mysql_query(sprintf('SHOW COLUMNS FROM %s', $table), $this->db);
5256
}
5357

5458
/**
@@ -58,7 +62,7 @@ function getColumns($table) {
5862
* @return resource A resultset resource
5963
*/
6064
function getRow($table, $where) {
61-
return mysql_query(sprintf('SELECT * FROM %s WHERE %s', $table, $where));
65+
return mysql_query(sprintf('SELECT * FROM %s WHERE %s', $table, $where));
6266
}
6367

6468
/**
@@ -68,15 +72,15 @@ function getRow($table, $where) {
6872
* @return resource A resultset resource
6973
*/
7074
function getTable($primary, $table) {
71-
return mysql_query(sprintf('SELECT %s FROM %s', $primary, $table));
75+
return mysql_query(sprintf('SELECT %s FROM %s', $primary, $table));
7276
}
7377

7478
/**
7579
* Get the tables in a database.
7680
* @return resource A resultset resource
7781
*/
7882
function getDatabase() {
79-
return mysql_query('SHOW TABLES');
83+
return mysql_query('SHOW TABLES');
8084
}
8185

8286
/**
@@ -87,7 +91,7 @@ function getDatabase() {
8791
* @return bool
8892
*/
8993
function updateRow($table, $values, $where) {
90-
return mysql_query(sprintf('UPDATE %s SET %s WHERE %s', $table, $values, $where));
94+
return mysql_query(sprintf('UPDATE %s SET %s WHERE %s', $table, $values, $where));
9195
}
9296

9397
/**
@@ -107,7 +111,7 @@ function insertRow($table, $names, $values) {
107111
* @return resource A resultset resource
108112
*/
109113
function deleteRow($table, $where) {
110-
return mysql_query(sprintf('DELETE FROM %s WHERE %s', $table, $where));
114+
return mysql_query(sprintf('DELETE FROM %s WHERE %s', $table, $where));
111115
}
112116

113117
/**
@@ -125,7 +129,7 @@ function escape($string) {
125129
* @return str[] An array of the fields and values from the next row in the resultset
126130
*/
127131
function row($resource) {
128-
return mysql_fetch_assoc($resource);
132+
return mysql_fetch_assoc($resource);
129133
}
130134

131135
/**
@@ -150,7 +154,7 @@ function numAffected() {
150154
* @return int The last insert ID
151155
*/
152156
function lastInsertId() {
153-
return mysql_insert_id();
157+
return mysql_insert_id();
154158
}
155159

156160
}

phprestsql.php

+2-13
Original file line numberDiff line numberDiff line change
@@ -137,29 +137,18 @@ function connect() {
137137
require_once($database.'.php');
138138
$this->db = new $database();
139139
if (isset($this->config['database']['username']) && isset($this->config['database']['password'])) {
140-
if (!$this->db->connect(
141-
$this->config['database']['server'],
142-
$this->config['database']['username'],
143-
$this->config['database']['password']
144-
)) {
140+
if (!$this->db->connect($this->config['database'])) {
145141
trigger_error('Could not connect to server', E_USER_ERROR);
146142
}
147143
} elseif (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
148-
if (!$this->db->connect(
149-
$this->config['database']['server'],
150-
$_SERVER['PHP_AUTH_USER'],
151-
$_SERVER['PHP_AUTH_PW']
152-
)) {
144+
if (!$this->db->connect($this->config['database'])) {
153145
$this->unauthorized();
154146
exit;
155147
}
156148
} else {
157149
$this->unauthorized();
158150
exit;
159151
}
160-
if (!$this->db->select_db($this->config['database']['database'])) {
161-
trigger_error('Could not select database', E_USER_ERROR);
162-
}
163152
}
164153

165154
/**

0 commit comments

Comments
 (0)