2015-11-20 46 views
0

我一直在使用PHP一段時間,但直到最近纔開始着眼於它的OOP方面。我想創建一個數據庫類,我可以存儲我的查詢功能,如更新,刪除,選擇,插入。在PHP中創建數據庫類

林不知道如果我試圖運行這個對什麼我做錯了,會有人來看看這個,給我一些建議,請

當我試圖運行此我得到以下錯誤雖然:

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) 

HTML:

<?php 
require_once("class.Database.php"); 
?> 
<html> 
    <head> 
    <title>Database's and Classes</title> 
    </head> 
    <body> 
    <?php 
     select("cars", "make"); 
    ?> 
    </body> 
</html> 

到目前爲止,我已經爲我的PHP:

<?php 
require_once __DIR__ . '../../../cfg/cfg.php'; 
class Database { 

private $dbConn; //stores the database connection 

public function __construct(){ 
    global $cfg; 

    $host = $cfg['db']=>host; 
    $user = $cfg['db']=>user; 
    $pass = $cfg['db']=>pass; 
    $db = $cfg['db']=>db; 

    mysqli_connect($host, $user, $pass, $db) or die("Couldn't connect"); 
} 

public function insert($table, $column, $value){ 
    $result = mysqli_query(INSERT into $this=>table($this=>column) VALUES ($this=>value)); 
    return $result; 
} 

public function select($table, $column){ 
    $result = mysqli_query(SELECT $this=>column FROM $this=>table); 
    return $result; 
} 

public function update($table, $column, $value, $whereCol, $whereVal){ 
    $result = mysqli_query(UPDATE $this=>table set $this=>column = $this=>value WHERE $this=>column = $this=>value); 
    return $result; 
} 

public function delete($table, $column, $value){ 
    $result = mysqli_query(DELETE FROM $this=>table WHERE $this=>column = $this=>value); 
    return $result; 
} 

} 

?> 
+3

我會從這裏開始http://php.net/manual/en/language.types.array.php之後繼續閱讀這個http://php.net/manual/en/language.oop5 .php – PeeHaa

+0

'=>'用於在數組中分配鍵::值對,' - >'用於從對象中獲取變量/屬性(即OOP)。 – Darren

回答

1

這只是在你的代碼簡單的語法錯誤:

$host = $cfg['db']=>host; 

這也意味着你有一個錯誤的位置:$this=>...,它應該是$this->....

正確地訪問這一點,你必須使用此代碼:

$host = $cfg['db']['host'];// or take a look at the example below if $cfg['db'] is an object 

只是一個小例子

// if $cfg['db'] is an array like below 
$cfg['db'] = array("host" => "test"); 
// you need to acces it this way 
$host = $cfg['db']['host']; 

// if $cfg['db'] is an object like below 
$cfg['db'] = new stdClass(); 
$cfg['db']->host = "test"; 
// you need to acces it this way 
$host = $cfg['db']->host;