gpt4 book ai didi

mysql - $wpdb->insert_id 不工作

转载 作者:行者123 更新时间:2023-11-30 22:08:22 38 4
gpt4 key购买 nike

我正在尝试获取最后插入的 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/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com