如何在PHP中从数据库中获取数据?
PHP中的数据库操作是 CRUD(创建、读取、更新和删除)操作中特别需要的非常关键的事情。
在本文中,我们将讨论读取部分,即从数据库中获取数据。
有两种方法可以使用PHP连接到数据库。它们如下。
- MySQLi (“i”代表改进)
- PDO (PHP数据对象)
MySQLi 与 PDO :这两种方法都非常好,但两种方法只有一个区别,PDO 可以在 12 个不同的数据库系统上工作,而 MySQLi 只能在 MySQL 数据库上工作。
连接到数据库:
MySQLi 面向对象
$conn = new mysqli($servername, $username, $databasename)
MySQLi 程序
$conn = mysqli_connect($servername, $username, $password, $databasename);
PDO
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password, $databasename);
执行查询:连接到数据库后,我们需要运行查询以获取数据。在读取操作中,我们将仅使用选择查询从数据库中获取数据。
MySQLi 面向对象
$conn->query($query);
MySQLi 程序
mysqli_query($conn, $query)
PDO
$stmt = $conn->prepare($query); $stmt->execute();
关闭连接:执行获取后,您应该使用 close()函数关闭与数据库的连接。
$conn->close();
样本数据库
在数据库中创建表:
CREATE TABLE `Student Details` (
`Roll_No` int(11) NOT NULL,
`Name` varchar(255) NOT NULL,
`City` varchar(255) NOT NULL,
`Age` int(11) NOT NULL,
PRIMARY KEY (`Roll_No`)
);
MySQLi 面向对象的方法:
PHP代码:
PHP
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL QUERY
$query = "SELECT * FROM `Student Details`;";
// FETCHING DATA FROM DATABASE
$result = $conn->query($query);
if ($result->num_rows > 0)
{
// OUTPUT DATA OF EACH ROW
while($row = $result->fetch_assoc())
{
echo "Roll No: " .
$row["Roll_No"]. " - Name: " .
$row["Name"]. " | City: " .
$row["City"]. " | Age: " .
$row["Age"]. "
";
}
}
else {
echo "0 results";
}
$conn->close();
?>
PHP
0) {
// OUTPUT DATA OF EACH ROW
while($row = mysqli_fetch_assoc($result)) {
echo "Roll No: " . $row["Roll_No"]
. " - Name: " . $row["Name"]. "
";
}
} else {
echo "0 results";
}
$conn->close();
?>
PHP
18;";
try
{
$conn = new PDO(
"mysql:host=$servername;dbname=$databasename",
$username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare($query);
// EXECUTING THE QUERY
$stmt->execute();
$r = $stmt->setFetchMode(PDO::FETCH_ASSOC);
// FETCHING DATA FROM DATABASE
$result = $stmt->fetchAll();
// OUTPUT DATA OF EACH ROW
foreach ($result as $row)
{
echo "Roll No: " . $row["Roll_No"]. " - Name: " .
$row["Name"]. " | City: " . $row["City"]. "
";
}
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn->close();
?>
输出:
Roll No: 1 - Name: Ram | City: Delhi | Age: 18
Roll No: 2 - Name: Shyam | City: Mumbai | Age: 19
Roll No: 3 - Name: Rohit | City: Chennai | Age: 18
Roll No: 4 - Name: Suresh | City: Kolkata | Age: 20
MySQLi 程序方法:
PHP代码:
PHP
0) {
// OUTPUT DATA OF EACH ROW
while($row = mysqli_fetch_assoc($result)) {
echo "Roll No: " . $row["Roll_No"]
. " - Name: " . $row["Name"]. "
";
}
} else {
echo "0 results";
}
$conn->close();
?>
输出:
Roll No: 1 - Name: Ram
Roll No: 2 - Name: Shyam
Roll No: 3 - Name: Rohit
Roll No: 4 - Name: Suresh
PDO 方法:
PHP代码:
PHP
18;";
try
{
$conn = new PDO(
"mysql:host=$servername;dbname=$databasename",
$username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare($query);
// EXECUTING THE QUERY
$stmt->execute();
$r = $stmt->setFetchMode(PDO::FETCH_ASSOC);
// FETCHING DATA FROM DATABASE
$result = $stmt->fetchAll();
// OUTPUT DATA OF EACH ROW
foreach ($result as $row)
{
echo "Roll No: " . $row["Roll_No"]. " - Name: " .
$row["Name"]. " | City: " . $row["City"]. "
";
}
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn->close();
?>
输出:
Roll No: 2 - Name: Shyam | City: Mumbai
Roll No: 4 - Name: Suresh | City: Kolkata