0

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

  • 1
    Use single quotes around `clive`, not back quotes. – Alex Howansky Apr 18 '18 at 14:40
  • also, i ihave tried within the foreach different ways of calling the array. {$result[firstname]}, with quotation marks and without, and also what you have seen in the first post –  Apr 18 '18 at 14:40
  • `echo "

    {$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
  • i noticed the missing semi colon. still didnt work. im new to this so in the parenthesis for the foreach the first input is the array, in this case $result and the second input is just a placeholder and can be whatever i want to call it right? –  Apr 18 '18 at 14:45
  • heres the updated version. its giving an error saying invalid arguement supplied for foreach –  Apr 18 '18 at 14:47
  • select($sql); if (sizeof($result) >= 1){ echo "query successful"; foreach($result as $value){ echo "

    {$value['first_name']}

    "; } } include 'view/footer.php'; ?>
    –  Apr 18 '18 at 14:47
  • You've got multiple issues here. Bad quotes n your query, bad quotes in your string, missing semi-colon. – John Conde Apr 18 '18 at 15:24
  • thanks for the input. i have fixed the missing semi colon. from my very basic understanding the sql query should use backticks other whan in the where clause which uses single quotes. then in the echo i should use double quotes so i can use single quotes inside the echo to use with the array. where am i going wrong? either way i dont think that fixes the php error its throwing saying invalid arguement in the foreach loop. i dont see whats wrong there though –  Apr 18 '18 at 16:02

0 Answers0