我试图将最后一个插入ID id的MySQL中,但它不返回任何内容。在数据库中插入行,并且$ wpdb-> last_query也会给出最后插入的查询。
这是我的代码

  global $wpdb;
  $table_name = $wpdb->prefix . "request_from";
  $wpdb->insert($table_name,
      array('pre' => $prefix,
          'first_name' => $first_name,
          'middle_name' => $middle_initial,
          'last_name' => $last_name,
          'badge_name' => $name_badge,
          'title' => $title,
          'company' => $company,
          'direct_mail' => $direct_mail,
          'twitter' => $twitter_handle,
          'direct_phone' => $direct_phone,
          'address' => $address,
          'address2' => $address_2,
          'city' => $city,
          'state' => $state,
          'province' => $province,
          'zip' => $zip_code,
          'country' => $country,
          'cc' => $cc,
          'cc_contact' => $second_contact,
          'cc_mail' => $second_email,
          'cc_phone' => $second_phone)
        );
$x= $wpdb->last_query;
$id = $wpdb->insert_id


我有一个名为id的列,具有自动增量值

id  int(11)         No  None    AUTO_INCREMENT

最佳答案

请遵循Codex中的wpdb参考进行故障排除:


通过$ wpdb-> show_errors()启用数据库错误显示
检查正在形成什么查询并通过$ wpdb-> last_query运行


请通过打印或转储变量检查代码,

   global $wpdb;
      $table_name = $wpdb->prefix . "request_from";
      $wpdb->insert($table_name,
          array('pre' => $prefix,
              'first_name' => $first_name,
              'middle_name' => $middle_initial,
              'last_name' => $last_name,
              'badge_name' => $name_badge,
              'title' => $title,
              'company' => $company,
              'direct_mail' => $direct_mail,
              'twitter' => $twitter_handle,
              'direct_phone' => $direct_phone,
              'address' => $address,
              'address2' => $address_2,
              'city' => $city,
              'state' => $state,
              'province' => $province,
              'zip' => $zip_code,
              'country' => $country,
              'cc' => $cc,
              'cc_contact' => $second_contact,
              'cc_mail' => $second_email,
              'cc_phone' => $second_phone)
            );
    $x= $wpdb->last_query;
    $id = $wpdb->insert_id;
// Let's output the last autogenerated ID.
  echo $wpdb->insert_id;

  // This returns the same result
  echo mysql_insert_id();

关于mysql - $ wpdb-> insert_id不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40928354/

10-13 08:53