0

I get the following error:

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

I checked the semicolons, brackets but a did not find anything missing. I am getting really frustrating, because I know that I am missing something but I do not know what.

Thank you for helping me!

<?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];
    }
  }
}
?>
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • function Update($table, $field, $data, $where = [{}]) , I am getting error here while running your code. – Affan Pathan Jul 13 '17 at 08:46
  • Your code is vulnerable to SQL injections. Please learn to use [prepared statements](https://www.youtube.com/watch?v=nLinqtCfhKY) instead. – tereško Jul 13 '17 at 08:48
  • I prepare my data somewhere else. Do not worry about it but about my code. – Tamás Őri Jul 13 '17 at 08:53
  • See the following answer for another question here on SO: https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them/18092267#18092267 – Shadow Jul 13 '17 at 08:58

2 Answers2

0

I could be wrong but my reputation isn't high enough to comment sorry but I thought with this function class would start after the connection info, i.e.

<?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];
    }
  }
}
    ?>
James
  • 190
  • 2
  • 4
  • 13
0

Could you more precise about the problem you have? I have tried to run your code and it seems fine. Here's the result

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
)
Rafi Ramadhan
  • 119
  • 1
  • 11