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];
}
}
}
?>
函數更新($ table,$ field,$ data,$ where = [{}]),我在運行代碼時出現錯誤。 –
您的代碼易受SQL注入攻擊。請學習使用[準備好的語句](https://www.youtube.com/watch?v=nLinqtCfhKY)。 –
我準備我的數據在其他地方。不要擔心,但關於我的代碼。 –