dougui5419 2012-10-05 19:37
浏览 39
已采纳

为什么我收到PDO查询错误?

Apologies in advance because I'm really unsure how to ask this question so if you need to know anything then please comment rather than downvote and I will edit.

I have teaser links on my main page which when clicked open up a window with the full article. I'm currently converting my MySQL code over to PDO and have gotten a little stuck.

In MySQL I used to be doing the following (Here, $foo_query is the query from the first page):

$id = $_GET['id'];

$sql = "SELECT id, postdate, title, body FROM FooBarTable WHERE id = $id";
if ($foo_query = mysql_query($sql)) {
    $r     = mysql_fetch_assoc($foo_query);
    $title = $r["title"];
    $body  = $r["body"];
}

Which is simple to understand to me. I've been trying to convert this using what I know, and it turns out I don't know very much. So far I have the following:

$id = $_GET['id'];

$sql = $DBH->prepare("SELECT id, postdate, title, body FROM FooBarTable WHERE id = :id OR id = $id");
$sql->bindParam(':id', $_REQUEST['id'], PDO::PARAM_INT);
if ($foo_query = $DBH->query($sql)) {
    $r->setFetchMode(PDO::FETCH_ASSOC);
    $r     = $foo_query->fetch();
    $title = $r["title"];
    $body  = $r["body"];
}
$sql->execute();

This brings up an error of 'PDO::query() expects parameter 1 to be string'. This is for the 'if' line.

Have I even written any of that PDO correctly? What would I need to do from here? A friend has recently taught me MySQL, but he doesn't know PDO at all which means I can't ask his advice (not all that helpful...)

  • 写回答

4条回答 默认 最新

  • douanye8442 2012-10-05 19:50
    关注

    This is the correct way, with comments:

    try {
        //Connect to the database, store the connection as a PDO object into $db.
        $db = new PDO("mysql:host=localhost;dbname=database", "user", "password");
    
        //PDO will throw PDOExceptions on errors, this means you don't need to explicitely check for errors.
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        //PDO will not emulate prepared statements. This solves some edge cases, and relives work from the PDO object.
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    
        //Prepare the statement.
        $statement = $db->prepare("SELECT id, postdate, title, body FROM FooBarTable WHERE id = :id");
        //Bind the Value, binding parameters should be used when the same query is run repeatedly with different parameters.
        $statement->bindValue(":id", $_GET['id'], PDO::PARAM_INT);
        //Execute the query
        $statement->execute();
    
        //Fetch all of the results.
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);
        //$result now contains the entire resultset from the query.
    }
    //In the case an error occurs, a PDOException will be thrown. We catch it here.
    catch (PDOException $e) {
        echo "An error has occurred: " . $e->getMessage();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效