本文介绍了执行时PDO插入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't find my mistake, I'm getting Execute fail error

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

推荐答案

DSN似乎不正确(其中有一个空格).试试这个PDO构造函数,并停止使用or die()

Looks like your DSN is incorrect (you have a space in it). Try this PDO constructor and stop using 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,但我觉得它很方便.

You don't have to set the default fetch mode to PDO::FETCH_ASSOC but I find it's handy.

这篇关于执行时PDO插入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:18