2
對於一個項目,我使用一個名爲meekrodb的mysql庫,並且我想用我自己的mysql函數創建一個擴展類,所以如果庫更新了,我只能重新編輯擴展類。Php:如何擴展庫?
作爲一個程序員開始我試圖擴展庫如下:
require './libs/meekrodb.2.0.class.php';
class Database_extend extends MeekroDB {
protected $db;
protected $database='';
function __construct($server=null, $user=null, $pass=null, $database=null)
{
// Creating a database object
$this->db= parent::__construct();
//$this->db= parent::__construct($server,$user,$pass,$database);
$this->database=$database;
} */
public function table_exists($tableName=null) {
if(is_null($tableName)){
trigger_error("Error in: ".__METHOD__." Missing table name.",E_USER_ERROR);
}
else{
$sql = "SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = '$this->database' AND table_name = '$tableName'";
$exists=$this->db->query($sql);
if($exists['count']>0)
{ return TRUE;}
else { return FALSE;}
}
}#-table_exists
如果我測試這個類(test.php的)
require './config.inc.php';
require './libs/Session.php';
require_once './libs/Ngram.class.php';
require_once './libs/Database_extend.class.php';
/*require_once './libs/meekrodb.2.0.class.php';*/
DB::$user=DB_USER;
DB::$password=DB_PASS;
DB::$dbName=DB_DATABASE;
$db1=new Database_extend(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
$t=$db1->query("SELECT * FROM temp LIMIT 0,1");
//var_dump($t); //THIS WORKS
var_dump($db1->table_exists('tem')); // THIS DOESN'T work
我收到了「呼叫到一個非對象錯誤的成員函數query(),這意味着db變量不是一個對象。
如何正確解決此問題?
我已經通過更改$ this-> db = parent :: __ construct()來解決該問題;到$ this-> db = new MeekroDB();並通過改變將Meekro擴展到DB。謝謝您的幫助。 – Eric