如何使用PHP成功连接到数据库? [英] How do I connect successfully to the database using PHP?
本文介绍了如何使用PHP成功连接到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好我的PHP代码连接到databasePlease帮助有问题。这是我的代码。
我尝试过:
Hi guys i have a problem with my php code for connecting to databasePlease help.Here's my code.
What I have tried:
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;
}
推荐答案
结果 = mysqli_query( SELECT DISTINCT类型FROM coffee)或 die(mysqli_connect_error());
result = mysqli_query("SELECT DISTINCT type FROM coffee") or die(mysqli_connect_error());
types = array();
// 从数据库中获取数据
while(
types = array(); //Get data from database while(
row = mysqli_fetch_array(
row = mysqli_fetch_array(
这篇关于如何使用PHP成功连接到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文