简体   繁体   English

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

[英]Why am I receiving a PDO query error?

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. 提前道歉,因为我真的不确定如何提出这个问题,所以如果你需要知道任何事情,那么请评论而不是downvote我会编辑。

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. 我目前正在将我的MySQL代码转换为PDO并且已经陷入困境。

In MySQL I used to be doing the following (Here, $foo_query is the query from the first page): 在MySQL中我曾经做过以下事情(这里,$ foo_query是来自第一页的查询):

$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'. 这会导致'PDO :: query()期望参数1为字符串'的错误。 This is for the 'if' line. 这是'if'行。

Have I even written any of that PDO correctly? 我是否正确地编写了任何PDO? 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...) 一位朋友最近教我MySQL,但他根本不懂PDO,这意味着我不能问他的建议(并非所有有用的......)

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();
}

You need to use PDOStatement::execute instead of PDO::query : 您需要使用PDOStatement::execute而不是PDO::query

$foo_query = $sql->execute();

You may also bind all your params at once when calling execute : 调用execute时,您也可以一次绑定所有参数:

$foo_query = $sql->execute(array(
    ':id' => $id
));

You should change it to: 您应该将其更改为:

$sql->execute();
if($r = $sql->fetch()) {
    $title = $r["title"];
    $body = $r["body"];

Try this: 尝试这个:

$sql = $DBH->prepare("SELECT id, postdate, title, body 
  FROM FooBarTable WHERE id = :id OR id = $id");
$sql->bindParam (':id', $_REQUEST['id'],PDO::PARAM_INT);
$sql->execute();

while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
  $title = $row["title"];
  $body = $row["body"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM