###前提・実現したいこと
当方WordPress初心者で、オリジナルのテーブルへのINSERTを試みています。
以下のソースコードにおいて、$wpdb->insert
がfalse
を返し、解決策を見つけることができません。
ご指南頂けましたら幸いです。
###発生している問題
以下ソースコードで
$rows = $wpdb->insert( $table_name, $update_info, $format_arr );
の$rows
がfalse
となります。
該当のソースコード
実行部分ですが、header.php で下記の流れを組んでいます。
php
<div class="phptest"> <?php $update_info = [ 'ip'=> '10.0.00.0', 'kinds_ID'=> 2, 'sentence'=> "hello", 'slug'=> "abc", 'users_ID'=> 1 ]; $format_arr; foreach ( $update_info as $v ) { $format_arr[] = my_get_sql_format($v); } var_dump($format_arr); $insert_id = my_insert_sql_row( 'wp_breads', $update_info ); var_dump($insert_id); ?> </div><!-- .phptest -->
上記には型を判定する関数my_get_sql_format()
と、挿入する関数my_insert_sql_row()
が含まれているので順に以下提示します。
まず型を判定する関数は下記です。(これは問題ございません。)
php
function my_get_sql_format( $v ){ $format; $type = gettype( $v ); if ( $type == 'string' ) { $format = '%s'; } elseif ( $type == 'integer' ) { $format = '%d'; } return $format; }
そして挿入する関数は下記です。
この関数の冒頭で戻り値$result
を定義しており、エラーの際はこの$result
に
$result['error_wrapper']['error_info']['エラー箇所'] = エラー内容;
のように入ることでエラー内容を検出する意図です。
今回の件では$rows
がfalse
になりますので
$result['error_wrapper']['error_info']['$rows'] = false;
が戻ることが問題となっています。
php
function my_insert_sql_row( $table_name, $update_info ){ global $wpdb; // エラー $result = [ 'error_wrapper'=>[ 'func' => 'my_insert_sql_row()' ,'args' => [ '$table_name'=>$table_name, '$update_info'=>$update_info ] ,'error_info' => [] ] ]; // 型を判定 foreach ( $update_info as $v ) { $format_arr[] = my_get_sql_format($v); } // 挿入を実行 $rows = $wpdb->insert( $table_name, $update_info, $format_arr ); // エラーがあれば取得して戻す if( ! $rows ){ $result['error_wrapper']['error_info']['$rows'] = $rows; $result['error_wrapper']['error_info']['last_query'] = $wpdb->last_query; $result['error_wrapper']['error_info']['last_error'] = $wpdb->last_error; $result['error_wrapper']['error_info']['show_errors'] = $wpdb->show_errors(); $result['error_wrapper']['error_info']['hide_errors'] = $wpdb->hide_errors(); }else{ $result = $wpdb->insert_id; } return $result; }
###試したこと
上記ソースコード後半のif
が下記ですが、このように各種エラーを出力するように書いたつもりです。
php
$result['error_wrapper']['error_info']['$rows'] = $rows; $result['error_wrapper']['error_info']['last_query'] = $wpdb->last_query; $result['error_wrapper']['error_info']['last_error'] = $wpdb->last_error; $result['error_wrapper']['error_info']['show_errors'] = $wpdb->show_errors(); $result['error_wrapper']['error_info']['hide_errors'] = $wpdb->hide_errors();
そしてここまで書いた流れを実行した結果(先述したheader.phpの出力結果)が下記HTMLとなりました。
ご覧のように詳しいエラーを出力してくれない状態です。
唯一last_query
にSHOW FULL COLUMNS FROM 'wp_breads'
という結果が出力されていますがこれも謎で、このようなSQL文はどこにも書いておらず困惑中です。
html
<div class="phptest"> array(5) { [0]=> string(2) "%s" [1]=> string(2) "%d" [2]=> string(2) "%s" [3]=> string(2) "%s" [4]=> string(2) "%d" } array(1) { ["error_wrapper"]=> array(3) { ["func"]=> string(19) "my_insert_sql_row()" ["args"]=> array(2) { ["$table_name"]=> string(9) "wp_breads" ["$update_info"]=> array(5) { ["ip"]=> string(9) "10.0.00.0" ["kinds_ID"]=> int(2) ["sentence"]=> string(5) "hello" ["slug"]=> string(3) "abc" ["users_ID"]=> int(1) } } ["error_info"]=> array(5) { ["$rows"]=> bool(false) ["last_query"]=> string(34) "SHOW FULL COLUMNS FROM `wp_breads`" ["last_error"]=> string(0) "" ["show_errors"]=> bool(false) ["hide_errors"]=> bool(true) } } } </div>
他にエラーを検出する方法をご存じの方や、ざっとソースコードをご覧になりあやしい原因を特定できる方がいらしたらご高察賜れますと幸甚に存じます。
自力ではここまでが限界で…なにとぞ、お力添え宜しくお願い申し上げます。
まだ回答がついていません
会員登録して回答してみよう