摘要:在本教程中,您将学习如何使用 PHP PDO 从 MySQL 数据库查询数据。您还将学习如何使用 PDO 准备好的语句来安全地选择数据。
PHP MySQL 使用简单的 SELECT 语句查询数据
要从 MySQL 数据库查询数据,请按照以下步骤操作:
首先,连接到 MySQL 数据库。查看使用 PDO 连接 MySQL 数据库教程以获取详细信息。
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
Code language: PHP (php)
然后,构造一条SELECT 语句并使用 PDO 对象的query()
方法执行它。
$sql = 'SELECT lastname, firstname, jobtitle
FROM employees
ORDER BY lastname';
$q = $pdo->query($sql);
Code language: PHP (php)
PDO 对象的query()
方法返回PDOStatement
对象,失败时返回false
。
接下来,使用setFetchMode()
方法设置PDOStatement
对象的PDO::FETCH_ASSOC
获取模式。 PDO::FETCH_ASSOC
模式指示fetch()
方法以按列名称索引的数组形式返回结果集。
$q->setFetchMode(PDO::FETCH_ASSOC);
Code language: PHP (php)
之后,使用PDOStatement
对象的fetch()
方法从结果集中获取每一行,直到没有剩余行为止。
把它们放在一起。
<?php
require_once 'dbconfig.php';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = 'SELECT lastname,
firstname,
jobtitle
FROM employees
ORDER BY lastname';
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Could not connect to the database $dbname :" . $e->getMessage());
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP MySQL Query Data Demo</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<div id="container">
<h1>Employees</h1>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Job Title</th>
</tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($row['lastname']) ?></td>
<td><?php echo htmlspecialchars($row['firstname']); ?></td>
<td><?php echo htmlspecialchars($row['jobtitle']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</body>
</div>
</html>
Code language: HTML, XML (xml)
PHP MySQL 使用 PDO 准备语句查询数据
在实践中,我们经常将参数从 PHP 传递给 SQL 语句,例如,获取姓氏以son
结尾的员工。为了安全地执行此操作并避免 SQL 注入攻击,您需要使用 PDO 准备语句。
让我们看一下下面的例子:
<?php
require_once 'dbconfig.php';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = 'SELECT lastname,
firstname,
jobtitle
FROM employees
WHERE lastname LIKE ?';
$q = $pdo->prepare($sql);
$q->execute(['%son']);
$q->setFetchMode(PDO::FETCH_ASSOC);
while ($r = $q->fetch()) {
echo sprintf('%s <br/>', $r['lastname']);
}
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
Code language: HTML, XML (xml)
脚本如何工作。
- 首先,我们在
SELECT
语句中使用问号 (?)。 PDO 将用相应的参数替换查询中的问号。问号称为位置占位符。 - 接下来,我们调用PDO对象的
prepare()
方法来准备执行的SQL语句。 - 然后,我们通过调用
PDOStatement
对象的execute()
方法来执行该语句。此外,我们还以数组形式传递一个参数来替换SELECT
语句中的占位符。通过这样做,SELECT
语句将被翻译如下:
SELECT lastname,
firstname,
jobtitle
FROM employees
WHERE lastname LIKE '%son';
Code language: SQL (Structured Query Language) (sql)
- 之后,我们设置
PDOStatement
对象的获取模式。 - 最后,我们获取结果集的每一行并显示姓氏字段。
PHP 为您提供了另一种在准备好的语句中使用占位符的方法,称为命名占位符。使用命名占位符的优点是:
- 更具描述性。
- 如果 SQL 语句有多个占位符,则将参数传递给
execute()
方法会更容易。
让我们看一下下面的例子:
<?php
require_once 'dbconfig.php';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = 'SELECT lastname,
firstname,
jobtitle
FROM employees
WHERE lastname LIKE :lname OR
firstname LIKE :fname;';
// prepare statement for execution
$q = $pdo->prepare($sql);
// pass values to the query and execute it
$q->execute([':fname' => 'Le%',
':lname' => '%son']);
$q->setFetchMode(PDO::FETCH_ASSOC);
// print out the result set
while ($r = $q->fetch()) {
echo sprintf('%s <br/>', $r['lastname']);
}
} catch (PDOException $e) {
die("Could not connect to the database $dbname :" . $e->getMessage());
}
Code language: HTML, XML (xml)
:lname
和:fname
是命名占位符。它们被我们传递给执行方法的关联数组中的相应参数替换。
在本教程中,您学习了如何使用 PDO 对象从 MySQL 数据库查询数据。
本教程有帮助吗?