2017-04-21 45 views
0

我將一個十進制值存儲在列數據類型爲十進制(12,2)的mysql數據庫中。使用PDO在mysql中存儲長十進制

如果我輸入10000,那就沒問題。但是,如果我想存儲100000個表,則表不存儲該值。

我的代碼

$query = $this->db->insert('donation', array(
      'donation_ref'  => $unique, 
      'user_id'   => $data['donorid'], 
      'donation_amt'  => $data['amount'], 
      'cause_id'   => $data['causes_i_want_to_support'], 
      'countrytospend' => $data['country_to_spend'], 
      'donation_type'  => $data['donation_type'], 
      'donation_prompt' => $data['donate_prompt'], 
      'gift_aid'   => $data['total'], 
      'donation_date'  => date('Y-m-d', strtotime($data['donation_date'])), 
      'giftaidyesno'  => $ga, 
      'donation_method' => $data['donation_method'] 
     )); 

public function insert($table, array $data) 
    { 
     ksort($data); 
$fieldNames = implode('`, `', array_keys($data)); 
    $fieldValues = ':' . implode(', :', array_keys($data)); 


     $sth = $this->prepare("INSERT INTO $table (`$fieldNames`) VALUES ($fieldValues)"); 


     foreach ($data as $key => $value) { 
      $sth->bindValue(":$key", $value); 
     } 

     $sth->execute(); 
    echo print_r($sth->errorInfo()); 
    } 

調試輸出

Insert prepared 
Insert executed 
object(PDOStatement)#32 (1) { ["queryString"]=> string(364) "INSERT INTO donation (`cause_id`, `countrytospend`, `donation_amt`, `donation_date`, `donation_method`, `donation_prompt`, `donation_ref`, `donation_type`, `gift_aid`, `giftaidyesno`, `user_id`) VALUES (:cause_id, :countrytospend, :donation_amt, :donation_date, :donation_method, :donation_prompt, :donation_ref, :donation_type, :gift_aid, :giftaidyesno, :user_id)" } 

錯誤處理 -

public function __construct($type, $host, $databaseName, $username, $password) 
    { 
     parent::__construct($type.':host='.$host.';dbname='.$databaseName.';charset=utf8', $username, $password); 
     $this->exec('SET CHARACTER SET utf8'); 

     if ($this->debug) { 
      $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 
     } 
    } 

    /** 
    * Enable/disable debug for database queries. 
    * @param $debug boolean TRUE to enable debug, FALSE otherwise. 
    */ 
    public function debug($debug) 
    { 
     $this->debug = $debug; 
    } 

沒有從MySQL年底錯誤,我可以插入相同的值在表中手動。但是,當我試圖通過PDO存儲它時,則不會出現錯誤,也不會存儲該行。

任何幫助,高度讚賞。

+0

執行插入操作後,您甚至不檢查mysql錯誤。這樣,你甚至不知道是否有任何錯誤... – Shadow

+0

@Shadow我已經更新了代碼。但這實際上並沒有產生任何錯誤(如果有的話)。 –

+0

您是否配置pdo在第一個地方拋出錯誤? – Shadow

回答

0

爲了記錄在案:

經過多次提意見建議得到查詢執行準確的錯誤消息,在OP發現問題實際上是由張貼值超出範圍比假設在不同的列引起的問題描述。