いつもお世話になっております。
複数の配列を、一つのprepareで、それぞれ別のループを回して、inset intoをさせようと考えております。具体的には以下のコードの通りです。
その結果、一つ目のループのみinsert into がテーブルに格納されていないことを確認したのですが、pdoではこのような処理は認められないのでしょうか?
また、このような複数の配列であるが同じ条件でプレースホルダを指定できるため、(下記でsecid,intperiod,periodがすべての配列で同じ条件で備わっているが、それぞれの配列の要素数が異なるため、それぞれforでループさせている)
場合には、PDO上、一般的にどのような処理をするべきなのでしょうか。
まだまだ初心者で申し訳ございませんが、
よろしくお願い申し上げます。
try { $pdo2 = new PDO($dsn, $user, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET `utf8`")); $pdo2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt2 = $pdo2->prepare ("INSERT INTO periodmaster (secid,intperiod,period) VALUES(:secid,:intperiod,:period)"); for ($iq=0; $iq < count($mixA); $iq++) { $stmt2->BindParam(':secid',$mixA[$iq][2],PDO::PARAM_INT); $stmt2->BindParam(':intperiod',$mixA[$iq][0],PDO::PARAM_INT); $stmt2->BindParam(':period',$mixA[$iq][1],PDO::PARAM_INT); $stmt2->execute(); } for ($iz=0; $iz < count($mixB); $iz++) { $stmt2->BindParam(':secid',$mixB[$iz][2],PDO::PARAM_INT); $stmt2->BindParam(':intperiod',$mixB[$iz][0],PDO::PARAM_INT); $stmt2->BindParam(':period',$mixB[$iz][1],PDO::PARAM_INT); $stmt2->execute(); } for ($io=0; $io < count($mixC); $io++) { $stmt2->BindParam(':secid',$mixB[$io][2],PDO::PARAM_INT); $stmt2->BindParam(':intperiod',$mixB[$io][0],PDO::PARAM_INT); $stmt2->BindParam(':period',$mixB[$io][1],PDO::PARAM_INT); $stmt2->execute(); } }catch (PDOException $e){ var_dump($e->getMessage()); }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。