Как успешно подключиться к базе данных с помощью PHP?
Привет, ребята, у меня есть проблема с моим php-кодом для подключения к databasePlease help.Вот мой код.
Что я уже пробовал:
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; }