[UPHPU] db.php
Gill Bates
unixgeek at zoelife4u.org
Sat May 6 09:19:31 MDT 2006
i see that the file didn't attach to my email so I'll have to paste it
here, sorry y'all:
db.php
<? // Database settings
function db_connect($dbname) {
$dbuser = "user";
$dbpass = "password";
$dbserver = "hostname";
$dbconnect = NULL;
$SQL = NULL;
global $dbconenct, $dbserver, $dbuser, $dbpass;
if (!$dbconnect) $dbconnect = mysql_connect($dbserver, $dbuser,
$dbpass);
if (!$dbconnect) {
return 0;
} elseif (!mysql_select_db($dbname)) {
return $dbconnect;
} //if
} //db_connect
/*$dbname = "zoelife";
$connection = mysql_connect($dbserver, $dbuser, $dbpass) || die
("Couldn't Connect to DB Server $dbserver: " . mysql_error());
mysql_select_db($dbname) or die ("Couldn't Select DB $dbname: " .
mysql_error());
$db = mysql_select_db($dbname) or die ("Couldn't Select DB $dbname: " .
mysql_error());
*/
// Start the database classes for use inside functions, fuckin pain in
the ass
set_error_handler('errorHandler');
function errorHandler ($errno, $errstr, $errfile, $errline, $errcontext)
// Anything from E_USER_ERROR and above, die();
{
switch($errno) {
case E_USER_WARNING:
case E_USER_NOTICE:
case E_WARNING:
case E_NOTICE:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
break;
case E_USER_ERROR:
case E_ERROR:
case E_PARSE:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
global $SQL;
// session_start();
if (eregi('^(sql)$', $errstr)) {
$errstr = "MySQL error: $MYSQL_ERRNO : $MYSQL_ERROR";
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
} else {
$SQL = NULL;
} //if
echo "<h2> This system is completely and utterly SPLEGACKED
!!!</h2>\n";
echo "<b><font color='red'>\n";
echo "<p>Fatal Error: $errstr (# $errno).</p>\n";
if ($SQL) echo "<p>SQL Query: $SQL</p>\n";
echo "<p>Error in line $errline of file '$errfile'. </p>\n";
echo "<p>Script:" . $_SERVER['PHP_FILE'] . "</p>\n";
echo "</font></b>";
session_unset();
session_destroy();
die();
default:
break;
} //switch
} //errorHandler
class dbTable {
var $tablename;
var $dbname;
var $rows_per_page;
var $pageno;
var $lastpage;
var $fieldlist;
var $data_array;
var $errors;
function dbTable () {
$this->tablename = 'home';
$this->dbname = 'zoelife';
$this->rows_per_page = 15;
$this->fieldlist = array('category', 'title', 'descr',
'content', 'posted', 'author');
//$this->fieldlist['id'] = array('pkey' => 'y');
} //Constructor
function getData ($where) {
$this->data_array = array();
$this->pageno = $pageno;
$this->rows_per_page = $rows_per_page;
$this->numrows = 0;
$this->lastpage = 0;
global $dbconnect, $SQL;
$dbconnect = db_connect($this->dbname) or trigger_error("SQL",
E_USER_ERROR);
if (empty($where)) {
$where_str = NULL;
} else {
$where_str = "WHERE $where";
} //if
$SQL = "SELECT count(*) FROM $this->tablename $where_str";
$result = mysql_query($SQL, $dbconnect) or trigger_error("SQL",
E_USER_ERROR);
$SQL_data = mysql_fetch_row($result);
$this->numrows = $query_data[0];
if($this->numrows <= 0) {
$this->pageno = 0;
return;
} //if
if ($rows_per_page > 0) {
$this->lastpage = ceil($this->numrows/$rows_per_page);
} else {
$this->lastpage = 1;
} //if
if ($pageno == '' || $pageno <= '1') {
$pageno = 1;
} elseif ($pagenp > $this->lastpage) {
$pageno = $this->lastpage;
} //if
$this->pageno = $pageno;
if ($rows_per_page > 0) {
$limit_str = 'LIMIT' . ($pageno - 1) * $rows_per_page . ','
. $rows_per_page;
} else {
$limit_str = NULL;
} //if
$SQL = "SELECT * FROM $this->tablename $where_str $limit_str";
$result = mysql_query($SQL, $dbconnect) or trigger_error("SQL",
E_USER_ERROR);
while ($row = mysql_fetch_assoc($result)) {
$this->data_array[] = $row;
} //while
mysql_free_result($result);
return $this->data_array;
} // getData
function insertRecord ($fieldarray) {
$this->errors = array();
global $dbconnect, $SQL;
$dbconnect = db_connect($this->dbname) or trigger_error("SQL",
E_USER_ERROR);
$fieldlist = $this->fieldlist;
foreach ($fieldarray as $field => $fieldvalue) {
if (!in_array($field, $fieldlist)) {
unset ($fieldarray[$field]);
} //if
} //foreach
$SQL = "INSERT INTO $this->tablename SET ";
foreach ($fieldarray as $item => $value) {
$SQL .= "$item='$value', ";
} //foreach
$SQL = rttrim($SQL, ', ');
$result = @mysql_query($SQL, $dbconnect);
if (mysql_errno() <> 0) {
if (mysql_errno() == 1062) {
$this->errors[] = "A Record Already exists with this ID.";
} else {
trigger_error("SQL", E_USER_ERROR);
} //if
} //if
return;
} //insertRecord
function updateRecord ($fieldarray) {
$this->errors = array();
global $dbconnect, $SQL;
$dbconnect = db_connect($this->dbname) or trigger_error("SQL",
E_USER_ERROR);
$fieldlist = $this->fieldlist;
foreach ($fieldarray as $field => $fieldvalue) {
if (!in_array($field, $fieldlist)) {
unset ($fieldarray[$field]);
} //if
} //foreach
$where = NULL;
$update = NULL;
foreach ($fieldarray as $item => $value) {
if (isset($fieldlist[$item]['pkey'])) {
$where .= "$item='$value' AND ";
} else {
$update .= "$item='$value', ";
}//if
}//foreach
$where = rtrim($where, ' AND ');
$result = mysql_query($SQL, $dbconnect) or trigger_error("SQL",
E_USER_ERROR);
return;
} //updateRecord
function deleteRecord ($fieldarray) {
$this->error = array();
global $dbconnect, $SQL;
$dbconnect = db_connect($this->dbname) or trigger_error("SQL",
E_USER_ERROR);
$fieldlist = $this->fieldlist;
$where = NULL;
foreach ($fieldarray as $item -> $value) {
if (isest($fieldlist[$item]['pkey'])) {
$where .= "$item='$value' AND ";
}
}
$where = rtrim($where, ' AND ');
$query = "DELETE FROM $this->tablename WHERE $where";
$result = mysql_query($SQL, $dbconnect) or trigger_error("SQL",
E_USER_ERROR);
return;
} //deleteRecord
} // dbTable Class
class articlesTable extends dbTable {
function articles() {
$this->tablename = 'articles';
$this->dbname = 'zoelife';
$this->rows_per_page = 15;
$this->fieldlist = array ('id', 'category', 'title', 'descr',
'content', 'posted', 'author');
$this->fieldlist['id'] = array('pkey' => 'y');
} //constructor
}
class blogTable extends dbTable {
function blog() {
$this->tablename = 'blog';
$this->dbname = 'zoelife';
$this->rows_per_page = 15;
$this->fieldlist = array ('id', 'category', 'title', 'descr',
'content', 'posted', 'author');
$this->fieldlist['id'] = array('pkey' => 'y');
} //constructor
}
class usersTable extends dbTable {
function users() {
$this->tablename = 'users';
$this->dbname = 'zoelife';
$this->rows_per_page = 15;
$this->fieldlist = array ('id', 'uid', 'pwd', 'fullname', 'email');
$this->fieldlist['id'] = array('pkey' => 'y');
} //constructor
}
class commentsTable extends dbTable {
function comments() {
$this->tablename = 'comments';
$this->dbname = 'zoelife';
$this->rows_per_page = 15;
$this->fieldlist = array ('id', 'comment', 'uid', 'commentdate');
$this->fieldlist['id'] = array('pkey' => 'y');
} //constructor
}
class quotesTable extends dbTable {
function quotes() {
$this->tablename = 'quotes';
$this->dbname = 'zoelife';
$this->rows_per_page = 15;
$this->fieldlist = array ('id', 'quoter', 'source', 'quote',
'quoted');
$this->fieldlist['id'] = array('pkey' => 'y');
} //constructor
}
class categoriesTable extends dbTable {
function categories() {
$this->tablename = 'categories';
$this->dbname = 'zoelife';
$this->rows_per_page = 15;
$this->fieldlist = array ('id', 'section', 'category');
$this->fieldlist['id'] = array('pkey' => 'y');
} //constructor
}
?>
I made an edit or two last night, thinking I could figure it out, namely
in the insertRecord function, I thought maybe since the id field is
auto_generated I could get away with taking it out of the insert record
method, thinking that the script was trippin cuz there was no data being
provided to insert into that particular field.
OK, I'm done posting emails, lol forgive my ignorance, I don't post to
mailing lists very often, I just read them.
More information about the UPHPU
mailing list