Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys i have a problem with my php code for connecting to databasePlease help.Here's my code.

What I have tried:

PHP
function  GetCoffeeTypes()
    {
        require 'Credentials.php';
        
        mysqli_connect('host', 'user','password') or die(mysqli_connect_error());
        mysqli_select_db('database');
        
        $result = mysqli_query("SELECT DISTINCT type FROM coffee") or die(mysqli_connect_error());
        
        $types = array();
        
        //Get data from database
        while($row = mysqli_fetch_array($result))
        {
            array_push($types, $row[0]);
        }
        
        //Close connection and return result
        mysqli_close();
        return $types;
    }
    
    //Get coffe objects from the database and return them in an array
    function GetCoffeeByType($type)
    {
        require 'Credentials.php';
        //Open connection and select database 
        mysqli_connect('host', 'user','password') or die(mysqli_connect_error());
        mysqli_select_db('database');
        $query = "SELECT * FROM coffee WHERE type LIKE '$type'";
        $result = mysqli_query($query) or die(mysqli_connect_error());
        $coffeeArray = array();
        
        //Get data from database
        while($row = mysqli_fetch_array($result))
        {
            $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 result
            
        }
        
        mysqli_close();
        return $coffeeArray;
    }
Posted
Updated 21-Nov-17 1:39am
v2

1 solution

You are using the old mysql syntax with the new mysqli extension (note the letter 'i'). The PHP mysqli extension offers two types of interfaces: Procedural and object-oriented. See PHP: Dual procedural and object-oriented interface - Manual[^] on how to convert from mysql to mysqli.

The converted code should look like (I have commented your existing code and changed it to be the old mysql syntax by removing the 'i'):
PHP
//mysql_connect('host', 'user','password') or die(mysql_connect_error());
//mysql_select_db('database');
$link = mysqli_connect('host', 'user', 'password', 'database');
if (mysqli_connect_error()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//$result = mysql_query("SELECT DISTINCT type FROM coffee") or die(mysql_connect_error());
if ($result = mysqli_query($link, "SELECT DISTINCT type FROM coffee")) {
    // Use $result here
    mysqli_free_result($result);
}
// ...
mysqli_close($link);

See also the example code provided on the PHP manual pages like PHP: mysqli::query - Manual[^].
 
Share this answer
 
v2
Comments
Member 12401110 22-Nov-17 3:04am    
Thanks for your help i really do appreciate

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900