前提・実現したいこと
当方超初心者です、よろしくお願いします。
phpで
$array = ["うどん","そば","ラーメン"];
という配列があるのですが、これを1行ずつMySQLにinsert する方法がわからず困っております。
該当のソースコード
php
1$array = ["うどん","そば","ラーメン"];
これを1行ずつmysqlに登録したいです。
試したこと
ここに問題に対して試したことを記載してください。
php
1try { 2 $pdo = new PDO($dbn, $user, $pwd); 3 } catch (PDOException $e) { 4 exit('dbError:'.$e->getMessage()); 5 } 6 7$idx = 0; 8$sql = ''; 9while ($idx < count($arr)) { 10 $sql .='INSERT INTO テーブル名 (id,word) VALUES (NULL,$arr[$idx])'; 11 $stmt = $pdo->prepare($sql); 12 $status = $stmt->execute(); 13 $idx = $idx + 1; 14 15 if ($status==false) { 16 $error = $stmt->errorInfo(); 17 exit('sqlError:'.$error[2]); 18 } else { 19 header('Location: index.php');
エラー内容
sqlError:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[$idx])' at line 1
補足情報(FW/ツールのバージョンなど)
超基礎的ですいません、「mysql php 配列」といったワードで検索しましたがよくわからず…
回答4件
あなたの回答
tips
プレビュー