更新mysql連接class

提供WOG各方面的技術問題,並提供最新path更新。

版主: 涅魂, 簫哥, 10度C~

stu6707
 
文章: 162
註冊時間: 2008-10-26 1:00 pm

更新mysql連接class

文章stu6707 » 2012-03-23 5:31 pm

因為wog3 裡的sql class有很多無用的功能,加上用static比較快 :face8: 所以就重新寫了一份sql class

使用方法跟之前的不大一樣

首先先建立新文件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);
等等


有任何問題或是錯誤請留言告知 :D




ETERNAL
 
文章: 2937
註冊時間: 2003-12-03 11:08 pm
性別: 男生

Re: 更新mysql連接class

文章ETERNAL » 2012-04-10 4:45 pm

不錯不錯喔

在水色混了幾年下來,感覺你功力一直往上提升了喔

static類型確實會比較快

WOG的mysql class最早是源自VBB,之後改版也是用當時新版的VBB mysql class

之後就一直沒改過了

在寫法上我還是比較建議用 DB_site->method_name

效能上跟 class_name::method_name 沒有差異

你可以寫一個迴圈測試看看


代碼: 選擇全部
$foo = new Foo();
$foo->bar();

這樣的寫法會比較像java


水色論壇 http://www.et99.net
簡恩峻分享

stu6707
 
文章: 162
註冊時間: 2008-10-26 1:00 pm

Re: 更新mysql連接class

文章stu6707 » 2012-04-10 6:33 pm

哈哈 為了看懂整個wog的架構當然要努力學習拉 XD

用了快1年才從門外漢到可以看懂wog寫法 :face6:

改成用靜態訪問是因為有些wog功能不用對資料庫做處理,而wog_act.php都會初始化與連結資料庫,很浪費資源

判斷是否需要連結資料庫又很麻煩,所以靜態訪問就比較方便拉

雖然引入了mysql.php,不過只有需要查詢資料庫時才會連接,可以省下不少資源




回到 Online FF Battle-WOG官方聯盟推廣處

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 11 位訪客