-1

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?

pgSystemTester
  • 8,979
  • 2
  • 23
  • 49
  • `\`clive\`` See https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-back-ticks-in-mysql – Syscall Apr 17 '18 at 20:38
  • FYI, it's more idiomatic to use `count()` rather than `sizeof()`. – Barmar Apr 17 '18 at 21:16

1 Answers1

1

$result is a 2-dimensional array. Each element of the array is a row of results, and the columns are elements of those associative arrays.

So you need to loop over the results:

foreach ($result as $row) {
    echo "<p>{$row['first_name'}</p>";
}

If you only need the result from the first row (e.g. if you know the query can't match more than one row), you can index it instead of looping:

echo "<p>{$result[0]['first_name']}</p>";

You also have the wrong kind of quotes around first_name in

$result[`first_name`]

They should be single or double quotes, not backticks. And in the SQL, you should have single or double quotes around clive, unless that's a column name. See When to use single quotes, double quotes, and back ticks in MySQL

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Beat me by 2 sec. Good answer. – Patrick Moore Apr 17 '18 at 20:39
  • i thought because of the while loop in the select query i wouldn't need to use a foreach loop in the index. can i not just access a single element like this echo "$result[0][first_name]" i thought ive seen that done before. how then can i access just a single value without a foreach loop? would i have to use a php function to convert them to variables? –  Apr 17 '18 at 20:57
  • yes, you can access a single row like that, but what if the query returns more than 1 row? – Barmar Apr 17 '18 at 21:15
  • Good point. So if I had a query that returned 3 rows with 3 columns, and I used fetch_assoc wouldn't that mean that row 1 would be the 0 position array, the second being at 1 and the third at 2. Now my understanding is that myarray[0] contains the first row with the columns as keys and the values would be the data stored. So does myarray[0][column1] not point to the first rows first column value? Sorry but I'm pretty new to back end stuff I hope you don't mind explaining –  Apr 17 '18 at 21:38