This could be a simple syntax error but I've tried every I know how. I have a database class for selecting data
<?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)){ // 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;
}
}
?>
I have instantiated this in index.php. I made a query and passed it to the select method then ran an if statement to say that if the size of the result is greater than or equal to 1 then echo query successfully. Here's where I keep screwing up. I'm trying to just print out the value of first_name but no matter what way I try it won't print. is this a 2d associative array?
<?php
include 'view/header.php';
include 'includes/connect.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";
echo "<p>{$result[`first_name`]}</p>";
}
include 'view/footer.php';
?>
I've tried it with different quotation marks, tried selecting 0 position then the first_name and it doesn't work. what am I missing?