I've created (with help) a class in PHP for connecting to and selecting info from a database. Here's the code:
<?php
/* this script is for creating a class to connect to the database */
include "includes/config.php";
class database {
protected static $connection; // variable to hold the mysqli connection
protected function connect(){
if (!isset(self::$connection)){
self::$connection = new mysqli(SERVER_NAME, USERNAME, PASSWORD, DB_NAME);
}
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 = new 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;
}
}
?>
The error occurs on the line just before the select
function which strangely has no code, but that's what the error says. Here is the exact error.
Parse error: syntax error, unexpected 'array' (T_ARRAY) in C:\wamp64\www\final\includes\connect.php on line 31
Can anyone spot the error?