From af65d827ad1d7e6bacc9fe0ac243f39dfffcd9d8 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Fri, 21 May 2004 03:59:17 +0000 Subject: [PATCH] Switch to MySQL - SQLite not working on main server... --- www/data/mxml.sql | 24 +++--- www/phplib/db-sqlite.php | 164 +++++++++++++++++++++++++++++++++++++++ www/phplib/db.php | 75 +++++++++--------- 3 files changed, 211 insertions(+), 52 deletions(-) create mode 100644 www/phplib/db-sqlite.php diff --git a/www/data/mxml.sql b/www/data/mxml.sql index ddb8561..bce99a5 100644 --- a/www/data/mxml.sql +++ b/www/data/mxml.sql @@ -1,5 +1,5 @@ -- --- "$Id: mxml.sql,v 1.6 2004/05/21 02:59:52 mike Exp $" +-- "$Id: mxml.sql,v 1.7 2004/05/21 03:59:17 mike Exp $" -- -- Database schema for the Mini-XML web pages. -- @@ -11,7 +11,7 @@ -- -- M. Sweet 05/17/2004 Initial revision. -- M. Sweet 05/19/2004 Added link, poll, and vote tables. --- +-- M. Sweet 05/20/2004 Changes for MySQL -- @@ -23,7 +23,7 @@ -- CREATE TABLE article ( - id INTEGER PRIMARY KEY, -- Article number + id INTEGER PRIMARY KEY AUTO_INCREMENT,-- Article number is_published INTEGER, -- 0 = private, 1 = public title VARCHAR(255), -- Title of article abstract VARCHAR(255), -- Plain text abstract of article @@ -45,7 +45,7 @@ CREATE TABLE article ( -- CREATE TABLE carboncopy ( - id INTEGER PRIMARY KEY, -- Carbon copy ID + id INTEGER PRIMARY KEY AUTO_INCREMENT,-- Carbon copy ID url VARCHAR(255), -- File or URL email VARCHAR(255) -- Email address ); @@ -60,7 +60,7 @@ CREATE TABLE carboncopy ( -- CREATE TABLE comment ( - id INTEGER PRIMARY KEY, -- Comment ID number + id INTEGER PRIMARY KEY AUTO_INCREMENT,-- Comment ID number parent_id INTEGER, -- Parent comment ID number (reply-to) status INTEGER, -- Moderation status, 0 = dead to 5 = great url VARCHAR(255), -- File/link this comment applies to @@ -79,7 +79,7 @@ CREATE TABLE comment ( -- CREATE TABLE link ( - id INTEGER PRIMARY KEY, -- Link ID number + id INTEGER PRIMARY KEY AUTO_INCREMENT,-- Link ID number parent_id INTEGER, -- Parent link ID or 0 for top-level is_category INTEGER, -- 0 = listing, 1 = category is_published INTEGER, -- 0 = private, 1 = public @@ -112,7 +112,7 @@ CREATE TABLE link ( -- CREATE TABLE poll ( - id INTEGER PRIMARY KEY, -- Poll ID number + id INTEGER PRIMARY KEY AUTO_INCREMENT,-- Poll ID number is_published INTEGER, -- 0 = private, 1 = public poll_type INTEGER, -- 0 = pick one, 1 = pick many question VARCHAR(255), -- Question plain text @@ -151,7 +151,7 @@ CREATE TABLE poll ( -- CREATE TABLE str ( - id INTEGER PRIMARY KEY, -- STR number + id INTEGER PRIMARY KEY AUTO_INCREMENT,-- STR number master_id INTEGER, -- "Duplicate of" number is_published INTEGER, -- 0 = private, 1 = public status INTEGER, -- 1 = closed/resolved, @@ -179,7 +179,7 @@ CREATE TABLE str ( -- CREATE TABLE strfile ( - id INTEGER PRIMARY KEY, -- File ID + id INTEGER PRIMARY KEY AUTO_INCREMENT,-- File ID str_id INTEGER, -- STR number is_published INTEGER, -- 0 = private, 1 = public filename VARCHAR(255), -- Name of file @@ -195,7 +195,7 @@ CREATE TABLE strfile ( -- CREATE TABLE strtext ( - id INTEGER PRIMARY KEY, -- Text ID + id INTEGER PRIMARY KEY AUTO_INCREMENT,-- Text ID str_id INTEGER, -- STR number is_published INTEGER, -- 0 = private, 1 = public contents TEXT, -- Text message @@ -213,7 +213,7 @@ CREATE TABLE strtext ( -- CREATE TABLE users ( - id INTEGER PRIMARY KEY, -- ID + id INTEGER PRIMARY KEY AUTO_INCREMENT,-- ID is_published INTEGER, -- 0 = private, 1 = public name VARCHAR(255), -- Login name email VARCHAR(255), -- Name/email address @@ -238,5 +238,5 @@ CREATE TABLE vote ( ); -- --- End of "$Id: mxml.sql,v 1.6 2004/05/21 02:59:52 mike Exp $". +-- End of "$Id: mxml.sql,v 1.7 2004/05/21 03:59:17 mike Exp $". -- diff --git a/www/phplib/db-sqlite.php b/www/phplib/db-sqlite.php new file mode 100644 index 0000000..5e91809 --- /dev/null +++ b/www/phplib/db-sqlite.php @@ -0,0 +1,164 @@ +Database error $sqlerr

\n"); + print("

Please report the problem to " + ."$DB_ADMIN.

\n"); + exit(1); +} + + +// +// 'db_close()' - Close the database. +// + +function +db_close() +{ + global $DB_CONN; + + + sqlite_close($DB_CONN); + $DB_CONN = false; +} + + +// +// 'db_count()' - Return the number of rows in a query result. +// + +function // O - Number of rows in result +db_count($result) // I - Result of query +{ + if ($result) + return (sqlite_num_rows($result)); + else + return (0); +} + + +// +// 'db_escape()' - Escape special chars in string for query. +// + +function // O - Quoted string +db_escape($str) // I - String +{ + return (sqlite_escape_string($str)); +} + + +// +// 'db_free()' - Free a database query result... +// + +function +db_free($result) // I - Result of query +{ + // Nothing to do, as SQLite doesn't free results... +} + + +// +// 'db_insert_id()' - Return the ID of the last inserted record. +// + +function // O - ID number +db_insert_id() +{ + global $DB_CONN; + + return (sqlite_last_insert_rowid($DB_CONN)); +} + + +// +// 'db_next()' - Fetch the next row of a result set and return it as an object. +// + +function // O - Row object or NULL at end +db_next($result) // I - Result of query +{ + if ($result) + return (sqlite_fetch_array($result, SQLITE_ASSOC, TRUE)); + else + return (NULL); +} + + +// +// 'db_query()' - Run a SQL query and return the result or 0 on error. +// + +function // O - Result of query or NULL +db_query($SQL_QUERY) // I - SQL query string +{ + global $DB_CONN; + +// print("

$SQL_QUERY

\n"); + + return (sqlite_query($DB_CONN, $SQL_QUERY)); +} + + +// +// 'db_seek()' - Seek to a specific row within a result. +// + +function // O - TRUE on success, FALSE otherwise +db_seek($result, // I - Result of query + $index = 0) // I - Row number (0 = first row) +{ + if ($result) + return (sqlite_seek($result, $index)); + else + return (FALSE); +} + + +// +// End of "$Id: db-sqlite.php,v 1.1 2004/05/21 03:59:17 mike Exp $". +// +?> diff --git a/www/phplib/db.php b/www/phplib/db.php index 20c32d3..793d459 100644 --- a/www/phplib/db.php +++ b/www/phplib/db.php @@ -1,6 +1,6 @@ Database error $sqlerr

\n"); - print("

Please report the problem to " - ."$DB_ADMIN.

\n"); - exit(1); + // Connected to server; select the database... + mysql_select_db($DB_NAME, $DB_CONN); } - - -// -// 'db_close()' - Close the database. -// - -function -db_close() +else { - global $DB_CONN; - + // Unable to connect; display an error message... + $sqlerrno = mysql_errno(); + $sqlerr = mysql_error(); - sqlite_close($DB_CONN); - $DB_CONN = false; + print("

Database error $sqlerrno: $sqlerr

\n"); + print("

Please report the problem to " + ."webmaster@easysw.com.

\n"); } @@ -73,7 +61,7 @@ function // O - Number of rows in result db_count($result) // I - Result of query { if ($result) - return (sqlite_num_rows($result)); + return (mysql_num_rows($result)); else return (0); } @@ -86,7 +74,7 @@ db_count($result) // I - Result of query function // O - Quoted string db_escape($str) // I - String { - return (sqlite_escape_string($str)); + return (mysql_escape_string($str)); } @@ -97,7 +85,8 @@ db_escape($str) // I - String function db_free($result) // I - Result of query { - // Nothing to do, as SQLite doesn't free results... + if ($result) + mysql_free_result($result); } @@ -110,7 +99,7 @@ db_insert_id() { global $DB_CONN; - return (sqlite_last_insert_rowid($DB_CONN)); + return (mysql_insert_id($DB_CONN)); } @@ -122,7 +111,7 @@ function // O - Row object or NULL at end db_next($result) // I - Result of query { if ($result) - return (sqlite_fetch_array($result, SQLITE_ASSOC, TRUE)); + return (mysql_fetch_array($result)); else return (NULL); } @@ -135,11 +124,17 @@ db_next($result) // I - Result of query function // O - Result of query or NULL db_query($SQL_QUERY) // I - SQL query string { - global $DB_CONN; + global $DB_NAME, $DB_CONN; -// print("

$SQL_QUERY

\n"); + return (mysql_query($SQL_QUERY, $DB_CONN)); - return (sqlite_query($DB_CONN, $SQL_QUERY)); +// print("

SQL_QUERY: $SQL_QUERY

\n"); +// +// $result = mysql_query($SQL_QUERY, $DB_CONN); +// $count = db_count($result); +// print("

Result = $count rows...

\n"); +// +// return ($result); } @@ -152,13 +147,13 @@ db_seek($result, // I - Result of query $index = 0) // I - Row number (0 = first row) { if ($result) - return (sqlite_seek($result, $index)); + return (mysql_data_seek($result, $index)); else return (FALSE); } // -// End of "$Id: db.php,v 1.4 2004/05/19 00:57:33 mike Exp $". +// End of "$Id: db.php,v 1.5 2004/05/21 03:59:17 mike Exp $". // ?>