実現したいこと
MySQLで、questionテーブルへレコードをインサートしたいです。
前提
MySQL
1CREATE TABLE IF NOT EXISTS `question` ( 2 `code` int(11) NOT NULL AUTO_INCREMENT, 3 `nitizi` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 4 `whose` int(4) NOT NULL COMMENT 'memberテーブルのcodeカラム', 5 `whom` int(4) NOT NULL COMMENT '誰への質問か', 6 `situation` varchar(500) NOT NULL, 7 `goal` varchar(500) DEFAULT NULL, 8 `what` varchar(500) DEFAULT NULL, 9 `why` varchar(500) DEFAULT NULL, 10 `try0` varchar(500) DEFAULT NULL, 11 `return1` varchar(2) DEFAULT NULL, 12 PRIMARY KEY (`code`) 13) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=109 ;
発生している問題・エラーメッセージ
エラーメッセージ SQLSTATE[42S02]: Base table or view not found: 1146 Table 'portfolio.latest' doesn't exist" ["string":"Exception":private]=> string(0) "" ["code":protected]=> string(5) "42S02" ["file":protected]=> string(57) "C:\xampp\htdocs\portfolio_作業用\new-db\new-select.php" ["line":protected]=> int(20) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(57) "C:\xampp\htdocs\portfolio_作業用\new-db\new-select.php" ["line"]=> int(20) ["function"]=> string(7) "execute" ["class"]=> string(12) "PDOStatement" ["type"]=> string(2) "->" ["args"]=> array(0) { } } [1]=> array(6) { ["file"]=> string(54) "C:\xampp\htdocs\portfolio_作業用\shitumon\hozon.php" ["line"]=> int(76) ["function"]=> string(7) "dbQuery" ["class"]=> string(7) "DbQuery" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(230) " INSERT INTO question (whose, whom, situation, goal, what, why, try0, return1) VALUES (112,110,'件名','','詳細','','その他','必要') " } } } ["previous":"Exception":private]=> NULL ["errorInfo"]=> array(3) { [0]=> string(5) "42S02" [1]=> int(1146) [2]=> string(38) "Table 'portfolio.latest' doesn't exist" } }
'portfolio.latest' doesn't exist" に関して latest を指定していないのに、インサートするテーブルに指定されていることになっているようです。理由が分かりません。
該当のソースコード
ソースコード①:クエリ部分(new-select.php)
function dbQuery($query) { $ConstDb = new ConstDb(); $dsn = ConstDb::dsn; $user = ConstDb::user; $password = ConstDb::password; try { $dbh = $ConstDb->ConnectDb($dsn, $user, $password); $stmt = $dbh->prepare($query); $stmt->execute(); $rec = $stmt->fetchAll(PDO::FETCH_ASSOC); $dbh = null; ⇒$recをreturn
上記の $query を出力した結果は、例えば以下のようになります。
INSERT INTO question (whose, whom, situation, goal, what, why, try0, return1) VALUES (112,110,'件名','','詳細','','その他','必要')
ソースコード②:クエリ文字列を①に渡す部分
require_once '../new-db/new-select.php'; $DBQuery = new DBQuery(); $DBQuery->dbQuery(' INSERT INTO question (whose, whom, situation, goal, what, why, try0, return1) VALUES ('.$honnin.','.$code.',\''.$situation.'\',\''.$goal.'\',\''.$what.'\',\''.$why.'\',\''.$try.'\',\''.$return1.'\') ');
試したこと
- select 文を代わりに実行して出力してみました。その際は想定動作(すべてのレコードが問題なく出力された)となりました。
- XAMPP自体の再インストールを実施しました。エラー内容に変化はありませんでした。
- 入れようとしている変数の値をベタ打ち(固定値)でインサートを行ってみましたが、エラー内容に変化はありませんでした。
- phpMyAdmin上で、実行しても同じエラーが出ます。(#1146 - 表 'portfolio.latest' は存在しません。)
- latest での検索結果(対象:作業フォルダ丸ごと)は該当なしでした。
補足情報(FW/ツールのバージョンなど)
XAMPPは最新バージョンを使用しています。
※8.0.3
テーブル名が不適切なものがありますが、
このエラーを直してから修正したいと思っていますのでご容赦ください。
new-select.php全文
php
1<?php 2require_once 'new-const.php'; 3 4class DbQuery { 5 6 // FETCH_ASSOC 7 function dbQuery($query) { 8 $ConstDb = new ConstDb(); 9 $dsn = ConstDb::dsn; 10 $user = ConstDb::user; 11 $password = ConstDb::password; 12 13 try { 14 $dbh = $ConstDb->ConnectDb($dsn, $user, $password); 15 $stmt = $dbh->prepare($query); 16 $stmt->execute(); 17 $rec = $stmt->fetchAll(PDO::FETCH_ASSOC); 18 $dbh = null; 19 } catch (Exception $e) { 20 var_dump($e); 21 exit($query); 22 } 23 return $rec; 24 } 25 26 // fetch_group 27 function selectFetchAll($query) { 28 $ConstDb = new ConstDb(); 29 $dsn = ConstDb::dsn; 30 $user = ConstDb::user; 31 $password = ConstDb::password; 32 try { 33 $dbh = $ConstDb->ConnectDb($dsn, $user, $password); 34 $stmt = $dbh->prepare($query); 35 $stmt->execute(); 36 $rec = $stmt->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP); 37 $dbh = null; 38 } catch (Exception $e) { 39 var_dump($e); 40 exit($query); 41 } 42 return $rec; 43 } 44}
new-const.php
(DBの接続方法などは適切でないと思ったので、別ブランチにて修正してあります。)
php
1<?php 2//データベースの定数の定義 3class ConstDb 4{ 5 public const dsn = '----割愛'; 6 public const user = '----割愛'; 7 public const password = '----割愛'; 8 9 const task = 'task'; 10 const bytime1 = 'bytime1'; 11 const bytime2 = 'bytime2'; 12 const emotion = 'emotion'; 13 const time1 = 'time1'; 14 const time2 = 'time2'; 15 const attention = 'attention'; 16 const strong1 = 'strong1'; 17 const strong2 = 'strong2'; 18 const strong3 = 'strong3'; 19 20 function ConnectDb($dsn, $user, $password) 21 { 22 $dsn = self::dsn; 23 $user = self::user; 24 $password = self::password; 25 try { 26 $dbh = new PDO($dsn, $user, $password, [ 27 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 28 PDO::ATTR_EMULATE_PREPARES, false 29 ]); 30 } catch (Exception $e) { 31 var_dump($e); 32 exit('接続エラーです。<a href="../registration/index.php">もどる</a>'); 33 } 34 return $dbh; 35 } 36}

回答1件
あなたの回答
tips
プレビュー