0

so i have a class created for handling sql select and connection stuff. when i create an instance of the class in my index page it throws an error

heres the db class code

include "includes/config.php";

 class database {
  protected static $connection; // variable to hold the mysqli connection


 protected function connect(){
  if (!isset(self::$connection)){ // if the connection variable is not set
      self::$connection = new mysqli(SERVER_NAME, USERNAME, PASSWORD, DB_NAME); // set the connection
  }
  if (self::$connection === false){ //in case connection failed return false
    return false;
  }
  else {
    return self::$connection; // returns the connection
  }
}



protected function query($query){ // public function to take a sql query
  $connection = $this->connect(); // calls the connect function to create a connection to the database
  $result = $connection->query($query); // puts the query into the result variable
  return $result; //returns the result
}



public function select($query){
  $rows = array();
  $result = $this->query($query);
  if($result === false){
    return false;
  }
  while($row = $result->fetch_assoc()){
        $rows[] = $row;
        }
    return $rows;
}



public function error(){
  $connection = $this->connect();
  return $connection->error;
   }
 }
 ?>

there error is from the index page on line 7 when i try to create a class instance

<?php

include 'view/header.php';
include 'includes/connect.php';
include 'inckudes/config.php'

$db = new database();

$sql = "SELECT `first_name`, `last_name` FROM `pratts_db`
      WHERE `first_name` = `clive`;"

$result = $db->select($sql);

if (sizeof($result) >= 1){
echo "query successful";
}

include 'view/footer.php';

?>

the exact error is as follows

Parse error: syntax error, unexpected '$db' (T_VARIABLE) in C:\wamp64\www\final\index.php on line 7

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • 6
    Missing `;` from `\`clive\`;"` – Nigel Ren Apr 17 '18 at 13:48
  • 1
    and from `include 'inckudes/config.php'` before that, which is what the error is actually about. – Federico klez Culloca Apr 17 '18 at 13:49
  • 1
    You misspelled *includes* as *inckudes* and forgot the semicolon at the end in line 5 ( `include 'inckudes/config.php'` => `include 'includes/config.php';` ) – Philipp Maurer Apr 17 '18 at 13:50
  • Also check which quotes your using in the SQL, backticks are for column/table names. – Nigel Ren Apr 17 '18 at 13:51
  • Try to use [PHP Storm](https://www.jetbrains.com/phpstorm/) to find your errors. Obviously you can use one of the many PHP linters available around. – shogitai Apr 17 '18 at 13:54
  • i will look into phpstorm in a bit. the query is now successful, thanks guys. one thing i have to ask, in the database class in the select method regarding the fetch_assoc() if the query used only has one row it is a standard 1d array? but if it has 2 or more rows, is it a 2d array? the lecturere wasnt to clear on that part. now im wondering how do i display the results of the query but the whole 2d array thing is confusing me. –  Apr 17 '18 at 14:05

0 Answers0