在以下代码中使用if(isset('prescription')){'prescription'}时遇到困难:
$query = "
SELECT
id,
full_name,
address,
email,
telephone,
prescription
FROM patients
WHERE id
LIKE '%" . $patient_id . "%'";
问题是,每当php从表字段中获取结果时,处方有时会为空,有时会带有信息..因此,此通知会显示:注意:未定义的索引:
我发现的解决方案只是在表单输入上使用isset而不是源自表的数据。因此,找到的示例包含$ _POST或$ _GET,这对我不起作用。
欢呼声, 伊恩
答案 0 :(得分:0)
我将在这里走出困境,猜测你正试图从空结果集中读取一个值。您应该首先检查有效结果。以下是使用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 ?>