2012-03-22 119 views
0

问题:为什么我得到Call to undefined function prepare()错误? 我该如何解决它?如何解决在PDO中调用未定义的函数prepare()?

我知道this与我的问题类似,但我不知道如何将答案应用于我的案例。

$myNull = null; 
$table="test_results"; 
$sql = "INSERT INTO $table (instance, uid, testid, quizstart, quizend, score) 
     VALUES (
       :instance, 
       :uid, 
       :testid, 
       :quizstart, 
       :quizend, 
       :score)"; 

try { 
    /*** connect to DB ***/ 
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password); 


    $stmt = $dbh>prepare($sql); // **************ERROR HERE**************** 

    $stmt->bindParam(':instance', $myNull, PDO::PARAM_INT);  
    $stmt->bindParam(':uid',$userID, PDO::PARAM_INT); 
    $stmt->bindParam(':testid', $tid, PDO::PARAM_STR); 
    $stmt->bindParam(':quizstart', $quizstart,PDO::PARAM_STR); 
    $stmt->bindParam(':quizend', $quizend,PDO::PARAM_STR); 
    $stmt->bindParam(':score', $score,PDO::PARAM_STR); 
    $stmt->execute(); 

    /*** display the id of the last INSERT ***/ 
    $lastInsertValue=$dbh->lastInsertId(); 

    $stmt->closeCursor(); 

    /*** close the database connection ***/ 
    $dbh = null; 
} 

回答

1

已经使用非运营商,而不是更大的箭头(->

$dbh>prepare($sql); //WRONG 
$dbh->prepare($sql); // CORRECT 
+0

我不能相信多少时间,我已经浪费在这样一个简单错误。非常感谢你的帮助! – TryHarder 2012-03-22 05:48:30

3

您错过了-。试试这个

$stmt = $dbh->prepare($sql); 
+0

感谢您的帮助:) – TryHarder 2012-03-22 05:48:51