host = $host; $this->user = $user; $this->password = $password; $this->database = $database; } function connect() { if (!$this->link) { $this->link = @mysql_connect($this->host, $this->user, $this->password, true); if (!$this->link) { $this->error('A connection could not be established with the database - ' . mysql_error(), true); } if (!@mysql_select_db($this->database, $this->link)) { $this->error('Selecting the database failed', true); } if ($GLOBALS['pageCharset'] == 'ISO-8859-1') { $charset = 'latin1'; } else if ($GLOBALS['pageCharset'] == 'UTF-8') { $charset = 'utf8'; } else { $charset = NULL; } if ($charset !== NULL) { if (function_exists('mysql_set_charset')) { mysql_set_charset($charset, $this->link); } else { mysql_query('SET NAMES ' . $charset); } } } } function error($query = 'N/A', $useQueryAsError = false) { if ($this->link) { $extra = mysql_errno($this->link) . ': ' . mysql_error($this->link); } else if (mysql_errno() != 0) { $extra = mysql_errno() . ': ' . mysql_error() . ' (no link)'; } else { $extra = ''; } if (function_exists('exitWithError') && $GLOBALS['dbErrorThrown'] == false) { $GLOBALS['dbErrorThrown'] = true; exitWithError('An error has occurred with the database', $query . "\n\n" . $extra); } else { header('HTTP/1.0 500 Internal Server Error'); exit('

I have an error:
' . htmlentities($useQueryAsError ? $query : $extra) . '

'); } } function escape($val) { $this->connect(); if (function_exists('mysql_real_escape_string')) { return mysql_real_escape_string($val, $this->link); } else if (function_exists('mysql_escape_string')) { return mysql_escape_string($val); } else { return addslashes($val); } } function escapeString($val) { return '"' . $this->escape($val) . '"'; } function escapeLike($val) { $val = $this->escape($val); $val = str_replace('_', '\_', $val); $val = str_replace('%', '\%', $val); return $val; } function escapeField($field) { $sqlField = '`' . str_replace('`', '', $field) . '`'; // Back tick is an illegal character $sqlField = str_replace('.', '`.`', $sqlField); // Allow table definition return $sqlField; } function query($query, $runDebug = true) { $this->connect(); if ($runDebug && function_exists('debugDatabase')) { $this->result = debugDatabase($this, $query); } else { $this->result = mysql_query($query, $this->link) or $this->error($query); } return $this->result; } function numRows($result = null) { if ($result === null) $result = $this->result; return mysql_num_rows($result); } function fetchAssoc($result = null) { if ($result === null) $result = $this->result; return mysql_fetch_assoc($result); } function fetchArray($result = null) { if ($result === null) $result = $this->result; return mysql_fetch_array($result); } function result($row, $col, $result = null) { if ($result === null) $result = $this->result; return mysql_result($result, $row, $col); } function insertId() { return mysql_insert_id($this->link); } function affectedRows() { return mysql_affected_rows($this->link); } function enumValues($sqlTable, $field) { $this->query('SHOW COLUMNS FROM ' . $sqlTable . ' LIKE "' . $this->escape($field) . '"'); if ($row = $this->fetchAssoc()) { return explode("','", preg_replace("/(enum|set)\('(.+?)'\)/", '\2', $row['Type'])); } else { $this->error('Could not return enum values for field "' . $field . '"'); } } function insert($sqlTable, $values, $onDuplicate = NULL) { $sqlFields = implode(', ', array_map(array($this, 'escapeField'), array_keys($values))); $sqlValues = implode(', ', array_map(array($this, 'escapeString'), $values)); if ($onDuplicate === NULL) { $this->result = $this->query('INSERT INTO ' . $sqlTable . ' ('. $sqlFields . ') VALUES (' . $sqlValues . ')'); } else if (!is_array($onDuplicate)) { $this->result = $this->query('INSERT INTO ' . $sqlTable . ' ('. $sqlFields . ') VALUES (' . $sqlValues . ') ON DUPLICATE KEY UPDATE ' . $onDuplicate); } else { $sqlSet = array(); foreach ($onDuplicate as $fieldName => $fieldValue) { $sqlSet[] = $this->escapeField($fieldName) . ' = ' . $this->escapeString($fieldValue); } $sqlSet = implode(', ', $sqlSet); $this->result = $this->query('INSERT INTO ' . $sqlTable . ' ('. $sqlFields . ') VALUES (' . $sqlValues . ') ON DUPLICATE KEY UPDATE ' . $sqlSet); } return $this->result; // insertId or affectedRows } function update($sqlTable, $values, $sqlWhere) { $sqlSet = array(); foreach ($values as $fieldName => $fieldValue) { $sqlSet[] = $this->escapeField($fieldName) . ' = ' . $this->escapeString($fieldValue); } $sqlSet = implode(', ', $sqlSet); $this->result = $this->query('UPDATE ' . $sqlTable . ' SET '. $sqlSet . ' WHERE ' . $sqlWhere); return $this->result; // affectedRows } function select($sqlTable, $fields, $sqlWhere, $limit = NULL) { if ($fields === 1) { $sqlFields = '1'; } else if ($fields === NULL) { $sqlFields = '*'; } else { $sqlFields = implode(', ', array_map(array($this, 'escapeField'), $fields)); } $sqlLimit = ($limit === NULL ? '' : ' LIMIT ' . intval($limit)); $this->result = $this->query('SELECT ' . $sqlFields . ' FROM ' . $sqlTable . ' WHERE ' . $sqlWhere . $sqlLimit); return $this->result; // numRows or fetchAssoc } function delete($sqlTable, $sqlWhere) { $this->result = $this->query('DELETE FROM ' . $sqlTable . ' WHERE ' . $sqlWhere); return $this->result; // affectedRows } } $GLOBALS['dbErrorThrown'] = false; // Common between multiple database class copies (=& not used) ?>