お世話になっています。
独自フレームワークを作成しており、PDOのinsert、update、delete文を簡易化したいなと思って
ConnectPdoクラスを作成していますが、update()のところでGeneral errorエラーが返ってきます。
err
1Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2031 in /var/www/html/myapp/app/core/lib/ConnectPdo.php:209 Stack trace: #0
流れは、以下の形でcontrollerから引数を渡してあげれば、
pdoのupdate文を実行させるようにしています。
(現在は、テストのためcontrollerで実行していますが、行く行くはmodelからConnectPdoクラスを使って実装します)
php
1function test() 2{ 3 $pdo = new ConnectPdo; 4 $pdo->connect_db(); 5 $table = 'test'; 6 $hash = array( 7 'name' => 'testtest', 8 'title' => 'テストです' 9 ); 10 $where = array('id' => 2, 'title' => 'fff'); 11 echo $pdo->update($table,$hash,$where); 12}
このエラーから下記のコードより「return $prepare->execute();」の部分でエラーが発生しています。
php
1<?php 2 /** 3 * update文 4 * hashはmust 5 * 6 * @param string $table table name 7 * @param array $hash setするカラムと値 8 * @param array $where where 9 * @param string $custom_cond カスタムwhere 10 * @return [type] [description] 11 */ 12 function update($table,$hash,$where=false,$custom_cond=false) 13 { 14 if(empty($hash)) return false; 15 16 /*---------- 17 * set組み立て 18 -----------*/ 19 $countHash = count($hash); 20 $array_keys = array_keys($hash); 21 $array_values = array_values($hash); 22 23 $implode_hash = ':'; 24 $implode_hash .= implode(' :',$array_keys); 25 26 $explode_hash = explode(' ',$implode_hash); 27 28 $i = 0; 29 $values = array(); 30 $sets_value = ''; 31 foreach($hash as $key => $value) { 32 $values[$i] = $value; 33 $sets_value .= $array_keys[$i] .' = ' .$explode_hash[$i]; 34 35 $i++; 36 37 if($i == $countHash) break; 38 39 $sets_value .= ','; 40 } 41 42 $sql = 'update ' .$table .' set ' .$sets_value; 43 44 45 46 /*---------- 47 * where組み立て 48 -----------*/ 49 if($where){ 50 $countWhere = count($where); 51 52 $keys_where = array_keys($where); 53 $values_where = array_values($where); 54 55 $keys_implode = ':'; 56 $keys_implode .= implode(' :',$keys_where); 57 58 $keys_explode = explode(' ',$keys_implode); 59 60 $wheres = ''; 61 $i = 0; 62 63 foreach($where as $key => $value) { 64 $wheres .= $keys_where[$i] .' = ' .$keys_explode[$i]; 65 $i++; 66 67 if($i == $countWhere) break; 68 69 $wheres .= ' and '; 70 } 71 72 $sql .= ' where ' .$wheres; 73 } 74 75 /*---------- 76 * PDO実行 77 -----------*/ 78 //custom_condがあった場合 79 if($custom_cond){ 80 $sql .= ' ' .$custom_cond; 81 return $this->_pdo->query($sql); 82 } 83 84 //custom_condがなかった場合は、prepareでexecute 85 $prepare = $this->_pdo->prepare($sql); 86 $array_values = array_values($hash); 87 88 // hash 89 for($i=0; $i<$countHash; $i++) { 90 if(is_numeric($array_values[$i])){ 91 $prepare->bindParam($explode_hash[$i],$array_values[$i],PDO::PARAM_INT); 92 }else{ 93 $prepare->bindParam($explode_hash[$i],$array_values[$i],PDO::PARAM_STR); 94 } 95 } 96 97 // where 98 if($where) { 99 for($j=0; $j<$countWhere; $j++) { 100 if(is_numeric($values_where[$j])){ 101 $prepare->bindValue($keys_explode[$j],$values_where[$j],PDO::PARAM_INT); 102 }else{ 103 $prepare->bindValue($keys_explode[$j],$values_where[$j],PDO::PARAM_STR); 104 } 105 } 106 } 107 108 return $prepare->execute(); 109 }
(エラーチェックなどは、後ほど実装していくつもりです。)
$whereのところで以下のようにすると、update文が実行できますが、
なぜ他のwhereの条件が入ってくると、実行されないのか。
なかなか解決できずにいます。
php
1$where = array('id' => 2);
ちなみに、 **echo $sql ** を仕込むとsql文は以下の形で生成されます。
php
1echo $sql; 2//update test set name = :name,title = :title where id = :id and title = :title 3return $prepare->execute();
コードが見辛くて申し訳ございません。
お力を貸していただければ幸いでございます。
よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー