2011-05-23 109 views
0

工作,我一直在使用這個類:PHP MySQL數據庫類不插入

<? 
class DbConnector { 

var $theQuery; 
var $link; 

function DbConnector() { 

    $host = 'localhost'; 
    $db = 'my_db'; 
    $user = 'root'; 
    $pass = 'password'; 

    // connect to the db 
    $this->link = mysql_connect($host, $user, $pass); 
    mysql_select_db($db); 
    register_shutdown_function(array(&$this, 'close')); 
} 

function find($query) { 
    $ret = mysql_query($query, $this->link); 
    if (mysql_num_rows($ret) == 0) 
     return array(); 
    $retArray = array(); 

    while ($row = mysql_fetch_array($ret)) 
     $retArray[] = $row; 

    return $retArray; 
} 

function insert($query) { 
    $ret = mysql_query($query, $this->link); 

    if (mysql_affected_rows() < 1) 
     return false; 
    return true; 
} 

function query($query) { 
    $this->theQuery = $query; 
    return mysql_query($query, $this->link); 
} 

// get some results 
function fetchArray($result) { 

    return mysql_fetch_array($result); 
} 

function close() { 
    mysql_close($this->link); 
} 

function exists($query) { 
    $ret = mysql_query($query, $this->link); 
    if (mysql_num_rows($ret) == 0) 
     return false; 
} 

function last_id($query) { 
    return mysql_insert_id($query); 
} 

} 

?> 

其中我使用了大量的查詢和它的正常工作,但我想用這個來插入查詢:

global $db; 
     $query = $db->insert("INSERT INTO `submissions` (
     `id` , 
     `quote` , 
     `filename` , 
     `date_added` , 
     `uploaded_ip` 
     ) 
    VALUES (
    NULL , '{$quote}', '{$filename}', NOW(), '{$_SERVER['REMOTE_ADDR']}') 
    "); 

而且它給我這個錯誤:

Not Found 

The requested URL /horh_new/id/<br /><b>Fatal error</b>: Call to a member function  insert() on a non-object in <b>/Applications/MAMP/htdocs/horh_new/addit.php</b> on line <b>52</b><br /> was not found on this server. 

雖然當我不使用上面這個類,並用此代替:

$con = mysql_connect("localhost","root","password"); 
if (!$con) { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("my_db", $con); 

mysql_query("INSERT INTO `submissions` (
     `id` , 
     `quote` , 
     `filename` , 
     `date_added` , 
     `uploaded_ip` 
     ) 
    VALUES (
    NULL , '{$quote}', '{$filename}', NOW(), '{$_SERVER['REMOTE_ADDR']}') 
    "); 

echo mysql_insert_id(); 

mysql_close($con); 

它工作正常。任何人都可以告訴我我班有什麼問題嗎?我真的很感激它。

+0

那麼從那個錯誤我會說$ db不是你所期望的。轉儲出來,你看到了什麼? – martynthewolf 2011-05-23 08:39:37

+0

您的$ db變量在您遇到該錯誤的位置未被實例化。 – Repox 2011-05-23 08:40:17

回答

2

你應該做的像

global $db; 
$db = new DbConnector(); 

或者更好的方式來使用,這是

添加類DbConnector的靜態功能,這將在整個請求

public static function getInstance(){ 
    static $instance = null; 
    if($instance === null){ 
     $instance = new DbConnector(); 
    } 

    return $instance;  
} 
只創建一個實例

並使用此類作爲

$db = DbConnector::getInstance(); 
$db->insert($query); 
+0

所以'$ db = DbConnector :: getInstance();'基本上只是檢查一個實例是否已經創建或尚未?感謝您的快速幫助! – 2011-05-23 10:36:29

+0

這將創建新的實例,如果它尚未被創建,否則它將返回舊的實例。在這種情況下__constructor不應該是公開的。 – Gaurav 2011-05-23 10:53:54

2

你需要使用它

$db = new DbConnetor(); 

現在你可以做一個插入

雖然我建議增加一個__construct方法連接到數據庫的第一

class DbConnector { 

    public function __construct() { 
    // connect to db here 

    } 
0

是之前實例化對象你確定$ db包含你的類的一個實例嗎?也許只是在調用$ db-> insert()之前執行$ db的var_dump()並查看它是否已設置。