【问题标题】:id on echo is found but not for other things找到了 echo 上的 id,但没有找到其他东西
【发布时间】:2013-07-29 11:09:13
【问题描述】:

所以当我这样做时

<?php echo $_GET['id']; ?>

我得到一个结果:1

但是当我将它用于我的查询时,我得到了错误

This discussion does not exists.

我复制粘贴了一点代码,因为这个问题不需要其余的代码。 这是我的代码:

if(isset($_GET['id']))
{
    $id = $_GET['id'];
    //We get the title and the narators of the discussion
    $req1 = $db->prepare('select title, user1, user2 from pm where id="'.$id.'" and id2="1"');
    $req1->execute();
    $dn1 = $req1->fetch();
    echo $dn1['title'];

    {
       echo '<div class="message">This discussion does not exists.</div>';
    }
}

【问题讨论】:

  • 您可以使用 PHPMyAdmin 测试查询吗? select title, user1, user2 from pm where id="1" and id2="1"
  • 你能告诉你connection code或参考这个php.net/manual/en/mysqli-stmt.fetch.php。还要检查您的数据库中是否有数据。使用if else 显示titleno record found message
  • 最后一个块运行不管,你忘了添加条件吗?
  • 如果您不使用 bind() 或将参数传递给 execute() 来绑定参数,那么使用 prepare() 是没有意义的。
  • @RohanKumar 我没有使用 mysql1 :s

标签: php html pdo


【解决方案1】:

你做错了。

$sql = 'select title, user1, user2 from pm where id=? and id2=1';
$req = $db->prepare($sql);
$req->execute(array($_GET['id']));
$row = $req1->fetch();
if (!$row)
{
    echo '<div class="message">This discussion does not exists.</div>';
}

还要确保您按照PDO tag wiki 中的说明进行连接

【讨论】:

  • 谢谢!现在像魅力一样工作。
【解决方案2】:

确保您的数据库中有针对提到的 $id 的正确记录。

请使用 echo $dn1->title;

AND“此讨论不存在。”代码块出现在您的 IF 块中。

在不同的块中写入 else 条件。

将您的最后一个查询打印为字符串并解决问题。

愉快的测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2016-09-17
    相关资源
    最近更新 更多