使用方法跟之前的不大一樣
首先先建立新文件mysql.php,放到forum_support資料夾裡
- 代碼: 選擇全部
複製全部到mysql.php
<?php
abstract class mysql
{
private static $connection=false;
private static function connect()
{
$hostname='hostname';//host server name
$username='username';//user name to login the server
$password='password';//password for the user
$database='database';//selected database name
if(!self::$connection = mysql_connect($hostname,$username,$password))//failed to connect to the db and alert error to users then exit the script
{
alertWindowMsg('資料庫連結失敗');
}
if(!mysql_select_db($database,self::$connection))//failed to connect to the selecting db and alert error to users then exit the script
{
alertWindowMsg('資料庫連結失敗');
}
mysql_query("SET NAMES 'utf8'",self::$connection);//set db to unicode
mysql_query("SET CHARACTER SET utf8",self::$connection);
mysql_query("SET CHARACTER_SET_CLIENT=utf8",self::$connection);
mysql_query("SET CHARACTER_SET_RESULTS=utf8",self::$connection);
mysql_query("SET CHARACTER_SET_CONNECTION=utf8",self::$connection);
}
public static function affected_rows()//return the affected rows of the query
{
return mysql_affected_rows(self::$connection);
}
public static function query($sql)//do a query
{
if(!self::$connection)//connect to the db if not connected
{
self::connect();
}
$resource = mysql_query($sql, self::$connection);//send the query
if(!$resource)//query is wrong and generate error
{
self::gen_err('Invalid SQL: '.$sql);
}
return $resource;//return results
}
public static function fetch_array($resource)
{
return mysql_fetch_array($resource);
}
public static function free_result($resource)
{
return mysql_free_result($resource);
}
public static function query_first($sql)
{
if(!self::$connection)
{
self::connect();
}
$resource = self::query($sql);
$returnarray = self::fetch_array($resource);
self::free_result($resource);
return $returnarray;
}
public static function insert_id()
{
return mysql_insert_id(self::$connection);
}
public static function close()
{
return mysql_close(self::$connection);
}
private static function gen_err($msg)
{
$user_id=isset($_COOKIE["wog_cookie"])?$_COOKIE["wog_cookie"]:'unknown';
$message=array(
'Database error in '.$msg,
'mysql error: '.mysql_error(self::$connection),
'mysql error number: '.mysql_errno(self::$connection),
'Date: '.date('l dS of F Y h:i:s A'),
"Script: http://$_SERVER[HTTP_HOST]",
'Referer: '.getenv("HTTP_REFERER"),
'p_id: '.$user_id,
'IP Address: '.get_ip()
);
$file='./db_error_log_'.date('Y_M').'.txt';
$text=implode("\r\n",$message)."\r\n\r\n\r\n";
if(!file_exists($file))
{
file_put_contents($file,$text,LOCK_EX);
}else
{
while(!is_readable($file)){}
file_put_contents($file,$text,FILE_APPEND | LOCK_EX);
}
alertWindowMsg('查詢資料庫出現錯誤');
}
}
./forum_support/config 資料夾可以整個刪除,裡面的2個文件都沒用了
如果有自行對那2個文件修改過,請把修改過的程式碼移出來
再來
- 代碼: 選擇全部
in global.php
刪除
unset($dbservertype);
//load config
require('./forum_support/config/config.php');
// init db **********************
// load db class
$dbservertype = strtolower($dbservertype);
$dbclassname="./forum_support/config/db_$dbservertype.php";
require($dbclassname);
$DB_site=new DB_Sql_vb;
$DB_site->appname='WOG';
$DB_site->appshortname='WOG';
$DB_site->database=$dbname;
$DB_site->connect($servername, $dbusername, $dbpassword, $usepconnect);
$DB_site->query_first("SET NAMES utf8;");
$DB_site->query_first("SET CHARACTER_SET_CLIENT=utf8;");
$DB_site->query_first("SET CHARACTER_SET_RESULTS=utf8;");
unset($servername, $dbusername, $dbpassword, $usepconnect);
//$DB_site->connect();
$dbpassword="";
$DB_site->password="";
- 代碼: 選擇全部
in wog_act.php, wog_etc.php, wog_fight.php
require_once("./forum_support/global.php");
下行加上
require('./forum_support/mysql.php');
- 代碼: 選擇全部
every php files in class folder
刪除 global $DB_site,xxxxxx的$DB_site,因為沒有這全域變量了
全部 $DB_site-> 替換成 mysql::
如果要使用新的mysql class裡的method
不是使用 DB_site->method_name了,是 class_name::method_name (我上傳的class方法是 - mysql::method_name)
所以說要send sql query使用方法是
- 代碼: 選擇全部
mysql::query(sql);
mysql::query_first(sql);
mysql::fetch_array(sql);
等等
有任何問題或是錯誤請留言告知