我有一个mysql数据库类,想知道如何在保持功能的同时将其转换为sqllite数据库类。
这是班级:
<?php
require_once("config.php");
class MySQLDatabase {
private $connection;
public $last_query;
private $magic_quotes_active;
private $real_escape_string_exists;
//This just opens a connection
function __construct() {
$this->open_connection();
$this->magic_quotes_active = get_magic_quotes_gpc();
$this->real_escape_string_exists = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
}
//This function opens the connection to MySQL
//it is called in the __construct()
public function open_connection() {
// 1. Create a database connection
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if (!$this->connection) {
die("Database connection failed: " . mysql_error());
}else {
$db_select = mysql_select_db(DB_NAME, $this->connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
}
}
//This just closes the connection
public function close_connection(){
// 5. Close connection
if(isset($this->connection)) {
mysql_close($this->connection);
unset($this->connection);
}
}
//This will do querys and will retrun the result if
//query was successfull
public function query($sql) {
$this->last_query = $sql;
$result = mysql_query($sql, $this->connection);
$this->confirm_query($result);
return $result;
}
//Preps values to be stored in MySql
public function escape_value( $value ) {
if( $this->real_escape_string_exists) { // PHP v4.3.0 or higher
// undo any magic quote effects so mysql_real_escape_string can do the work
if( $this->magic_quotes_active ) { $value = stripslashes( $value ); }
$value = mysql_real_escape_string( $value );
} else { // before PHP v4.3.0
// if magic quotes aren't already on then add slashes manually
if( !$this->magic_quotes_active ) { $value = addslashes( $value ); }
// if magic quotes are active, then the slashes already exist
}
return $value;
}
//This fetchs the mysql array and puts it into a php array
public function fetch_array($result) {
return mysql_fetch_array($result);
}
//This will return the number of rows
public function num_rows($result){
return mysql_num_rows($result);
}
//This will get the last id insert over the current bd connection
public function insert_id(){
return mysql_insert_id($this->connection);
}
//This will return the number of rows effected by this query
public function affected_rows() {
return mysql_affected_rows($this->connection);
}
//Confirms queries, is called in query()
private function confirm_query($result) {
if (!$result) {
$output = "Database query failed: " . mysql_error() . "<br />";
$output.= "Last SQL Query: ".$this->last_query;
die($output);
}
}
}
$database = new MySQLDatabase();
//This code should be used on pages that require a DB
//We load the database class with creates an
//instance of $database
//*require_once("../includes/database.php");
//Now we check to make sure that it worked
//**if(isset($database)) {echo "Database: true";} else{echo "Database: false";}
//**echo "<hr />";
?>
这是配置:
<?php
//Database Constants
defined('DB_SERVER') ? null : define("DB_SERVER", "private");
defined('DB_USER') ? null : define("DB_USER", "private");
defined('DB_PASS') ? null : define("DB_PASS" ,"private" );
defined('DB_NAME') ? null : define("DB_NAME", "private" );
?>