2015-11-20 73 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;