2017-07-13 66 views
0

PHP意外T_VARIABLE我得到以下錯誤:在mysqli的OOP編程

Parse error: syntax error, unexpected '$this' (T_VARIABLE) in /home2/totyaszerver/public_html/autoszallitoberles.hu/db.php on line 12.

我查了分號,括號但沒有發現任何東西丟失。我感到非常沮喪,因爲我知道我錯過了一些東西,但我不知道是什麼。

謝謝你幫助我!

<?php 
class KRDB{ 

    private $DB_NAME = "####################"; 
    private $DB_ADDRESS = "localhost"; 
    private $DB_USERNAME = "###############"; 
    private $DB_PASSWORD = '#############'; 
    private $krdb = ""; 

    function KRDB() 
    { 
    $this->krdb = new mysqli($this->DB_ADDRESS, //I get the error here//$this->DB_USERNAME, $this->DB_PASSWORD, $this->DB_NAME); 
    if ($this->krdb->connect_errno) { 
        echo "Failed to connect to MySQL: " . $this->krdb->connect_error; 
     exit(); 
    } 
    } 

    function Update($table, $field, $data, $where = [{}]){ 
    $generatedWhere = $this->generateWhere($where); 
    $sql = sprintf("UPDATE %s SET %s = '%s' WHERE %s", $table, $field, $data, $generatedWhere); 
    if ($this->krdb->query($sql) === TRUE) { 
     return true; 
    } 
    else{ 
     return false; 
    } 
    } 

    function Delete($table, $where = [{}]){ 
    $generatedWhere = $this->generateWhere($where); 
    $sql = sprintf("DELETE FROM %s WHERE %s", $table, $generatedWhere); 
    if ($this->krdb->query($sql) === TRUE) { 
     return true; 
    } 
    else{ 
     return false; 
    } 
    } 

    function Insert($table, $data) 
    { 
    $generatedFields = ""; 
    $generatedData = ""; 
    for ($i=0; $i < count($data); $i) { 
     $generatedFields .= sprintf(" %s ", $data[$i][0]); 
     $generatedData .= "'" . $data[$i][1] . "'"; 
     if($i != count($data)-1) 
     { 
     $generatedFields .= ", "; 
     $generatedData .= ", "; 
     } 
    } 
    $sql = "INSERT INTO %s(%s) VALUES(%s)"; 
    if ($this->krdb->query($sql) === TRUE) { 
     return true; 
    } 
    else{ 
     return false; 
    } 
    } 

    private function generateWhere($where) 
    { 
    $generatedWhere = ""; 
    for ($i=0; $i < count($where); $i) { 
     $generatedWhere .= sprintf(" %s = '%s' ", $where[$i][0], $where[$i][1]); 
     if($i != count($where)-1) 
     { 
     $generatedWhere .= " " . $where[$i][2] . " "; 
     } 
    } 
    return $generatedWhere; 
    } 

    function Select($table, $where, $fields = "*"){ 
    $generatedWhere = $this->generateWhere($where); 
    $sql = sprintf("SELECT %s FROM %s WHERE %s", $fields, $table, $generatedWhere); 
    $result = $this->krdb->query($sql); 
    $rows = []; 
    while($row = $result->fetch_assoc()) 
    { 
     $rows[] = $row; 
    } 
    return $rows; 
    } 
    function Count($table, $field, $where = [{}]) 
    { 
    $generatedWhere = $this->generateWhere($where); 
    $sql = sprintf("SELECT COUNT(%s) FROM %s WHERE %s", $field, $table, $generatedWhere); 
    $result = $this->krdb->query($sql); 
    $fieldC = sprintf("COUNT(%s)", $field); 
    while($row = $result->fetch_assoc()) 
    { 
     return $row[$fieldC]; 
    } 
    } 
} 
?> 
+0

函數更新($ table,$ field,$ data,$ where = [{}]),我在運行代碼時出現錯誤。 –

+0

您的代碼易受SQL注入攻擊。請學習使用[準備好的語句](https://www.youtube.com/watch?v=nLinqtCfhKY)。 –

+0

我準備我的數據在其他地方。不要擔心,但關於我的代碼。 –

回答

0

我可能是錯的,但我的聲望不夠高發表評論抱歉,我以爲這個功能類的連接信息之後將開始,即

<?php 

    private $DB_NAME = "####################"; 
    private $DB_ADDRESS = "localhost"; 
    private $DB_USERNAME = "###############"; 
    private $DB_PASSWORD = '#############'; 
    private $krdb = ""; 

class KRDB{ 

    function KRDB() 
    { 
    $this->krdb = new mysqli($this->DB_ADDRESS, //I get the error here//$this->DB_USERNAME, $this->DB_PASSWORD, $this->DB_NAME); 
    if ($this->krdb->connect_errno) { 
     echo "Failed to connect to MySQL: " . $this->krdb->connect_error; 
     exit(); 
    } 
    } 

    function Update($table, $field, $data, $where = [{}]){ 
    $generatedWhere = $this->generateWhere($where); 
    $sql = sprintf("UPDATE %s SET %s = '%s' WHERE %s", $table, $field, $data, $generatedWhere); 
    if ($this->krdb->query($sql) === TRUE) { 
     return true; 
    } 
    else{ 
     return false; 
    } 
    } 

    function Delete($table, $where = [{}]){ 
    $generatedWhere = $this->generateWhere($where); 
    $sql = sprintf("DELETE FROM %s WHERE %s", $table, $generatedWhere); 
    if ($this->krdb->query($sql) === TRUE) { 
     return true; 
    } 
    else{ 
     return false; 
    } 
    } 

    function Insert($table, $data) 
    { 
    $generatedFields = ""; 
    $generatedData = ""; 
    for ($i=0; $i < count($data); $i) { 
     $generatedFields .= sprintf(" %s ", $data[$i][0]); 
     $generatedData .= "'" . $data[$i][1] . "'"; 
     if($i != count($data)-1) 
     { 
     $generatedFields .= ", "; 
     $generatedData .= ", "; 
     } 
    } 
    $sql = "INSERT INTO %s(%s) VALUES(%s)"; 
    if ($this->krdb->query($sql) === TRUE) { 
     return true; 
    } 
    else{ 
     return false; 
    } 
    } 

    private function generateWhere($where) 
    { 
    $generatedWhere = ""; 
    for ($i=0; $i < count($where); $i) { 
     $generatedWhere .= sprintf(" %s = '%s' ", $where[$i][0], $where[$i][1]); 
     if($i != count($where)-1) 
     { 
     $generatedWhere .= " " . $where[$i][2] . " "; 
     } 
    } 
    return $generatedWhere; 
    } 

    function Select($table, $where, $fields = "*"){ 
    $generatedWhere = $this->generateWhere($where); 
    $sql = sprintf("SELECT %s FROM %s WHERE %s", $fields, $table, $generatedWhere); 
    $result = $this->krdb->query($sql); 
    $rows = []; 
    while($row = $result->fetch_assoc()) 
    { 
     $rows[] = $row; 
    } 
    return $rows; 
    } 
    function Count($table, $field, $where = [{}]) 
    { 
    $generatedWhere = $this->generateWhere($where); 
    $sql = sprintf("SELECT COUNT(%s) FROM %s WHERE %s", $field, $table, $generatedWhere); 
    $result = $this->krdb->query($sql); 
    $fieldC = sprintf("COUNT(%s)", $field); 
    while($row = $result->fetch_assoc()) 
    { 
     return $row[$fieldC]; 
    } 
    } 
} 
    ?> 
+0

不!它們是類屬性(如從類內部訪問它們時引用$ this所示,所以它們必須在類內部定義 –

+0

好吧,我的壞,謝謝指出它, – James

0

莫非你更精確的對你有問題嗎?我試圖運行你的代碼,它似乎很好。下面是結果

class KRDB{ 

    private $DB_NAME = "test_db"; 
    private $DB_ADDRESS = "localhost"; 
    private $DB_USERNAME = "test"; 
    private $DB_PASSWORD = 'test'; 
    private $krdb = ""; 

    function KRDB() 
    { 
     $this->krdb = new mysqli($this->DB_ADDRESS, $this->DB_USERNAME, $this->DB_PASSWORD, $this->DB_NAME); 
     if ($this->krdb->connect_errno) { 
      echo "Failed to connect to MySQL: " . $this->krdb->connect_error; 
      exit(); 
     } 
     else { 
      print_r($this->krdb); 
     } 
    } 
} 

$a = new KRDB(); 

// And it is working 
mysqli Object 
(
    [affected_rows] => 0 
    [client_info] => mysqlnd 5.0.12-dev - 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $ 
    [client_version] => 50012 
    [connect_errno] => 0 
    [connect_error] => 
    [errno] => 0 
    [error] => 
    [error_list] => Array 
     (
     ) 

    [field_count] => 0 
    [host_info] => Localhost via UNIX socket 
    [info] => 
    [insert_id] => 0 
    [server_info] => 5.7.18-0ubuntu0.16.04.1 
    [server_version] => 50718 
    [stat] => Uptime: 105254 Threads: 1 Questions: 15360 Slow queries: 0 Opens: 4770 Flush tables: 1 Open tables: 411 Queries per second avg: 0.145 
    [sqlstate] => 00000 
    [protocol_version] => 10 
    [thread_id] => 425 
    [warning_count] => 0 
)