the db has one row with 4 columns. i have selected 2 columns, first_name and last_name. i passed it through a select function in a db class, hows one result but it wont echo out
db class
<?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;
}
}
?>
and the script with the foreach loop
<?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";
foreach($result as $result){
echo "<p>{$result[0]['first_name']}</p>"
}
}
include 'view/footer.php';
?>
the error says 'expecting semicolon on line 17 but i put that in and still got the same error
Parse error: syntax error, unexpected '}', expecting ',' or ';' in C:\wamp64\www\final\index.php on line 17
{$result[0]['first_name']}
"` <- missing semi colon here. I didn't even bother checking for it. I copied it into PHPStorm and it told me where and what, you should do the same. A decent IDE does wonders. – Andrei Apr 18 '18 at 14:40{$value['first_name']}
"; } } include 'view/footer.php'; ?> – Apr 18 '18 at 14:47