【问题标题】:How to get data from row using id number in link using PDO prepared Statement [duplicate]如何使用 PDO 准备语句在链接中使用 ID 号从行中获取数据 [重复]
【发布时间】: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


【解决方案1】:

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

看看我在更改过程中所做的 cmets:

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

【讨论】:

  • 出现错误...解析错误:语法错误,第 17 行 file.php 中的文件意外结束
  • 你可能错过了一些右括号或类似的东西
  • 找到了。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
  • 2011-01-15
  • 2020-04-14
  • 2012-11-29
相关资源
最近更新 更多