PDO执行时插入错误

时间:2013-11-27 04:51:45

标签: php mysql pdo

我找不到我的错误,我收到了执行失败错误

$db = new PDO('mysql:host=localhost; dbname=xxxxxx', 'yyyyyy', 'zzzzzz', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")) or die ("fail");;
$query = "INSERT INTO multiTicker (mtgox,btcstamp,btce,btcchina,myDateTime) VALUES (:mtgox,:btcstamp,:btce,:btcchina,:myDateTime)";
$st = $db->prepare($query) or die ("Query fail");
$st->execute(array(':mtgox' => $mtgox,
               ':btcstamp' => $btcstamp,
               ':btce' => $btce,
               ':btcchina' => $btcchina,
               ':myDateTime' => $myDateTime)) or die ("Execute fail");

1 个答案:

答案 0 :(得分:0)

您的DSN看起来不正确(您有一个空格)。试试这个PDO构造函数,停止使用or die()

$db = new PDO('mysql:host=localhost;dbname=xxxxxx;charset=utf8', 'yyyyyy', 'zzzzzz', array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES   => false,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));

$query = "INSERT INTO multiTicker (mtgox,btcstamp,btce,btcchina,myDateTime) VALUES (:mtgox,:btcstamp,:btce,:btcchina,:myDateTime)";
$st = $db->prepare($query);
$st->execute(array(
    ':mtgox' => $mtgox,
    ':btcstamp' => $btcstamp,
    ':btce' => $btce,
    ':btcchina' => $btcchina,
    ':myDateTime' => $myDateTime
));

您不必将默认提取模式设置为PDO::FETCH_ASSOC,但我发现它很方便。