如何使用 PDO 准备语句从 link 中使用 ID 号的行获取数据

How to get data from row using id number in link using PDO prepared Statement

问题是我试图只使用该行的 ID 号获取一行,然后能够在 html 中回显结果。
我在 link.
中传递 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...

非常感谢您的反馈和建议,以更正错误和我犯的任何其他错误

如果我没理解错的话,你的问题是询问如何在你的示例中正确使用准备好的语句。让我来回答这个问题。

看看我对我的更改所做的评论:

<?php
include "/includes/db.php";
$id = $_GET['id'];
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,        // enable PDO errors
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,   // fetch associative arrays by default
    PDO::ATTR_EMULATE_PREPARES => false,                // Use native prepared statements
];
//                      You should always specify charset VVVVV
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password, $options);

// Prepare and execute SQL passing in the value of $id
$MyFAQlist = $pdo->prepare('SELECT * FROM MyFAQlist WHERE id = ? ORDER BY visits DESC');
$MyFAQlist->execute([$id]);
?>

您可以简单地使用 foreach 而不是 while 循环:

<?php foreach($MyFAQlist as $row): ?>