【发布时间】:2019-12-16 07:28:36
【问题描述】:
问题是我试图只使用该行的 id 号获取一行,然后能够在 html 中回显结果。
我在链接中传递 ID 号。
示例: link.php?id=55
我已经搜索并搜索了示例,并且我发现了许多脚本示例可供参考。
<?php
include("/includes/db.php");
$id=$_GET['id'];
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = 'SELECT * FROM MyFAQlist WHERE id = '.$id.' ORDER BY visits DESC';
$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>
<meta name="robots" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Link</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome-animation/0.2.1/font-awesome-animation.css" integrity="sha256-3NjHxD73dx5Pf2EgnPZPlzE+/KcUEhyR2kaGPH7vGCc=" crossorigin="anonymous" />
</head>
<body>
<div id="container">
<h1>LINK INFORMATION</h1>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>ID</th>
<th>VISITS</th>
<th>DATE</th>
<th>NAME</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($row['id']) ?></td>
<td><?php echo htmlspecialchars($row['visits']) ?></td>
<td><?php echo htmlspecialchars($row['reg_date']) ?></td>
<td><?php echo "<a target=\"_blank\" rel=\"noopener\" href=\"" . htmlspecialchars($row['TXTurl']). "\" >" . htmlspecialchars($row['TXTlinkname']). "</a>"; ?></td>
<td><textarea class="form-control clip Spud-Bud-clip" onclick="this.focus();this.select()"rows="2" cols="50"><a target="_blank" rel="noopener" href="<?php echo htmlspecialchars($row['TXTurl']) ?>"><?php echo htmlspecialchars($row['TXTlinkname']) ?></a></textarea></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</body>
</div>
</html>
我得到它的工作,但我在第 8 行遇到错误
Fatal error: Uncaught Error: Call to a member function setFetchMode() on boolean in...
感谢您的反馈和建议,以纠正错误和我犯的任何其他错误
【问题讨论】:
-
这里没有prepared statements。查询失败,这就是为什么您没有得到 PDOStatement 对象作为回报。
-
另外,如果我没记错的话,显示的代码包含一个sql注入漏洞。
$id参数没有转义,直接取自邪恶的外部用户数据$_GET['id']。
标签: php mysql pdo prepared-statement