php - 如何使用 PDO 准备好的语句使用链接中的 id 号从行获取数据

标签 php mysql pdo prepared-statement

问题是我试图使用该行的 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...

非常感谢您提供反馈和建议来纠正我所犯的错误和任何其他错误

最佳答案

如果我理解正确,您的问题是询问如何在您的示例中正确使用准备好的语句。我来回答一下这个问题。

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

<?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): ?>

关于php - 如何使用 PDO 准备好的语句使用链接中的 id 号从行获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57415080/

相关文章:

sql - 这会锁定数据库吗?

php - 将 .mdb 数据插入 SQL 时出现编码问题

PHP PDO 查询,参数位于数组中

php - 安排 cron 作业 laravel

php - 使用 PHP Curl 协助 USPS API

mysql - 错误代码 :1452 while inserting elements in table

用于记录 PPC AD 日常性能的 MySQL DB 架构

php - $_SESSION 对 sql 注入(inject)安全吗?

PHP:为客户端创建唯一标识符(独立于时间、cookie 和 IP 号)

php - 从另一个表插入数据到mysql表