2012-10-05 19 views
2

提前道歉,因爲我真的不確定如何提出這個問題,所以如果你需要知道任何事情,請評論而不是downvote,我會編輯。爲什麼我收到PDO查詢錯誤?

我在我的主頁上有傳情鏈接,點擊打開窗口時會顯示整篇文章。我目前正在將我的MySQL代碼轉換爲PDO,並且稍微卡住了。

在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"]; 
} 

這很容易理解我。我一直在試圖用我所知道的來轉換它,結果我知道的並不多。到目前爲止,我有以下幾點:

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

這就提出了一個錯誤「PDO ::查詢()預計參數1是字符串」。這是'if'行。

我是否已經正確書寫了任何PDO?我需要從這裏做什麼?一位朋友最近教我的MySQL,但他不知道PDO在所有這意味着我可以不問他的意見(不是所有的有用......)

+1

你需要在'$ sql'運行查詢,而不是直接對數據庫句柄。 – andrewsi

+1

如果您只是要將用戶輸入直接注入到它們中,您爲什麼還要使用準備好的語句? 'OR id = $ id'不應該在那裏...如果你想在那裏出於某種原因,把它作爲一個參數,並像其他':id'那樣綁定它。 – cHao

回答

5

這是正確的做法,與評論:

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

我可以問爲什麼它全部包含在try/catch循環中嗎?它是否總是被包含在內,或者是否存在你已經完成的特定原因?請注意,在回顯$ result [「title」](例如)之後,這實際上返回了一個空白查詢。 – tomdot

+0

當我的網站上線時,我是否也認爲我不應該設置ERRMODE_EXCEPTION? – tomdot

+1

@tomdot:你錯了。應始終保持ERRMODE_EXCEPTION。這樣,PDO拋出PDOExceptions,它是可捕獲的(使用'try/catch'塊),它允許你以任何你想要的方式處理錯誤。閱讀更多關於[例外和嘗試/趕上這裏](http://il.php.net/manual/en/language.exceptions.php) –

2

您需要使用PDOStatement::execute,而不是PDO::query

$foo_query = $sql->execute(); 

調用execute時,您還可以結合所有的PARAMS一次:

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

這是行不通的,你只能在執行中使用'PDO :: PARAM_STR'類型的參數。 – JvdBerg

+0

MySQL將自動轉換爲INT。 – bfavaretto

+1

在手冊中:'一組數值與執行SQL語句中綁定參數的元素數量一樣多。所有值都被視爲PDO :: PARAM_STR' – JvdBerg

1

您應將其更改爲:

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

試試這個:

$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"]; 
} 
相關問題