-1

So i was following a tutorial on how i can create a website with a database using php and wamp

and am failing on connecting my website to my database

this is the code i wrote

<?php

require ("Entities/CoffeeEntity.php");

class CoffeeModel {

    function GetCoffeeTypes() {
        require 'Credentials.php';

 $mysqli = new mysqli("$servername", "$username", "$password", "$dbname");
        $result = $mysqli->query("Select a distinct Cofffee");
        $types = array();

        while ($row = $result->fetch_assoc()) {
            array_push($types, $row[0]);
        }

        mysql_close();
        return $types;
    }

    function GetCoffeeByType($type) {
        require 'Credentials.php';

        $mysqli = new mysqli("$servername", "$username", "$password", "$dbname");

        $query = "SELECT * FROM coffee WHERE type LIKE '$type'";
        $result = $mysqli->query("error");
        $coffeeArray = array();

        while ($row = $result->fetch_assoc()) {
            $name = $row[1];
            $type = $row[2];
            $price = $row[3];
            $roast = $row[4];
            $country = $row[5];
            $image = $row[6];
            $review = $row[7];

            $coffee = new CoffeeEntity(-1, $name, $type, $price, $roast, $country, $image, $review);
            array_push($coffeeArray, $coffee);
        }
      
        mysql_close();
        return $coffeeArray;
    }

}

?>

and this is the error that i get the error

Edit: ok so i figured out that i need to replace mysql to mysqli

and i tried but it still seems not working cause i guess am doing something wrong

this is the result i came through

<?php

require ("Entities/CoffeeEntity.php");

//Contains database related code for the Coffee page.
class CoffeeModel {

    //Get all coffee types from the database and return them in an array.
    function GetCoffeeTypes() {
        require 'Credentials.php';

        //Open connection and Select database.   
  $mysqli = new mysqli("$servername", "$username", "$password", "$dbname");
        $result = $mysqli->query("Select a distinct Cofffee");
        $types = array();

        //Get data from database.
        while ($row = $result->fetch_assoc()) {
            array_push($types, $row[0]);
        }

        //Close connection and return result.
        mysql_close();
        return $types;
    }

    //Get coffeeEntity objects from the database and return them in an array.
    function GetCoffeeByType($type) {
        require 'Credentials.php';

        //Open connection and Select database.     
        $mysqli = new mysqli("$servername", "$username", "$password", "$dbname");

        $query = "SELECT * FROM coffee WHERE type LIKE '$type'";
        $result = $mysqli->query("error");
        $coffeeArray = array();

        //Get data from database.
        while ($row = $result->fetch_assoc()) {
            $name = $row[1];
            $type = $row[2];
            $price = $row[3];
            $roast = $row[4];
            $country = $row[5];
            $image = $row[6];
            $review = $row[7];

            //Create coffee objects and store them in an array.
            $coffee = new CoffeeEntity(-1, $name, $type, $price, $roast, $country, $image, $review);
            array_push($coffeeArray, $coffee);
        }
        //Close connection and return result
        mysql_close();
        return $coffeeArray;
    }

}

?>

and this error shows up

error2

can someone please help and make it work (sorry am really bad at this )

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • all i need is for someone to correct my first code to be in mysqli form or PDO form – iTzQusai x Apr 04 '20 at 05:29
  • I'm voting to close this question as off-topic because you are seeking a coding service. SO users will provide help when you get stuck, but apparently you have not even started. – YvesLeBorg Apr 04 '20 at 10:38
  • [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Apr 04 '20 at 11:15

1 Answers1

-1

The query() method returns false one failure, see: https://www.php.net/manual/en/mysqli.query.php

You must check the method's result before calling fetch_assoc().

Your query is failing because the first argument of the method must be a valid query while you are passing the string error, because of that you are trying to call a method on a boolean.

Try changing line 36 with $result = $mysqli->query($query);

cn7r66
  • 164
  • 1
  • 5