2012-03-28 84 views
0

我是pdo的新手,不明白爲什麼下面的插入查詢不起作用。如果我刪除執行該查詢的行,那麼當然不會插入,但不會有錯誤。如果我離開那條線,腳本不會被執行。當然,我檢查並重新檢查了表名和字段名稱。希望有人能幫我理解。請注意,在執行查詢之前,我的表的ber_mBacth_date字段被設置爲NULL。乾杯。馬克PDO SQL - 更新查詢問題

<?php 
$db_host = 'localhost'; 
$db_user = 'user'; 
$db_password = 'user'; 
$db_database = 'myconsole';    

$mBatchDate = date('Y-m-d H:i:s'); 

$connexion = new PDO("mysql:host=$db_host;dbname=$db_database", $db_user, $db_password); 
$qry = $connexion->execute('UPDATE batcherrors SET ber_mBatch_date = "'.$mBatchDate.'"'); 

$connexion = NULL; 
?> 

回答

1

你可以嘗試,而不是:

$connexion = new PDO("mysql:host=$db_host;dbname=$db_database", $db_user, $db_password); 
$qry = $connexion->execute('UPDATE batcherrors SET ber_mBatch_date = "'.$mBatchDate.'"'); 

做:

$statement = $connexion->prepare("UPDATE batcherrors SET ber_mBatch_date = :mBatchDate"); 
$statement->bindValue(':mBatchDate', $mBatchDate, PDO::PARAM_STR); 
$statement->execute(); 

Binding建議的方式來設置參數的值(在連接)。

+0

謝謝Michal ... – Marc 2012-03-28 12:37:51