我将在这里冒险并猜测您正在尝试从空结果集中读取一个值。你应该首先检查一个有效的结果。这是一个使用 PDO 的示例
$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
// I've changed your query to remove the LIKE comparison
// Seemed incorrect for an ID lookup
$stmt = $pdo->prepare('SELECT
id, full_name, address, email, telephone, prescription
FROM patients WHERE id = ?');
$stmt->bindParam(1, $patient_id);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Check for any results
if (0 === count($results)) {
throw new Exception('No results found');
}
foreach ($results as $result) : ?>
<dl>
<dt>ID</dt>
<dd><?= htmlspecialchars($result['id']) ?></dd>
<dt>Full name</dt>
<dd><?= htmlspecialchars($result['full_name']) ?></dd>
<!-- and so on -->
</dl>
<?php endforeach ?>