admin管理员组文章数量:1332389
I am trying to do some from a custom plugin in tables of WP-ERP plugin. I am using the following code:
global $wpdb;
$wpdb->show_errors( true );
$table_name= $wpdb->prefix.'erp_acct_products';
$wpdb->print_error();
$DBPresults= $wpdb->get_results("SELECT name FROM $table_name WHERE id=1");
$wpdb->print_error();
echo "<p>User count is {$DBPresults}</p>";
$wpdb->print_error();
?>
and it load this errors:
WordPress database error: []
SELECT option_value FROM wp_options WHERE option_name = 'pm_migration_start_2_3' LIMIT 1
WordPress database error: []
SELECT name FROM wp_erp_acct_products WHERE id=1
User count is Array
WordPress database error: []
Any idea??
SELECT name FROM wp_erp_acct_products WHERE id=1
I am trying to do some from a custom plugin in tables of WP-ERP plugin. I am using the following code:
global $wpdb;
$wpdb->show_errors( true );
$table_name= $wpdb->prefix.'erp_acct_products';
$wpdb->print_error();
$DBPresults= $wpdb->get_results("SELECT name FROM $table_name WHERE id=1");
$wpdb->print_error();
echo "<p>User count is {$DBPresults}</p>";
$wpdb->print_error();
?>
and it load this errors:
WordPress database error: []
SELECT option_value FROM wp_options WHERE option_name = 'pm_migration_start_2_3' LIMIT 1
WordPress database error: []
SELECT name FROM wp_erp_acct_products WHERE id=1
User count is Array
WordPress database error: []
Any idea??
SELECT name FROM wp_erp_acct_products WHERE id=1
Share
Improve this question
edited Jul 6, 2020 at 8:16
Jacob Peattie
44.1k10 gold badges50 silver badges64 bronze badges
asked Jul 4, 2020 at 9:48
Christoforos KotsiosChristoforos Kotsios
11 bronze badge
3
|
1 Answer
Reset to default 0It looks like your SQL succeeded to me. So I guess your problem is that you're getting an array of results (which is probably an array of arrays: rows, then columns per row) when you were expecting a single scalar value.
Try using get_var() to get a single value from the database:
$DBPresults = $wpdb->get_var( "SELECT name FROM $table_name WHERE id=1" );
本文标签: wpdbQuery Problem in Clustom Plugin
版权声明:本文标题:wpdb - Query Problem in Clustom Plugin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289751a2447573.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
pm_migration_start_2_3
seems to be completely unrelated to your code. What is the structure of thewp_erp_acct_products
table? – Jacob Peattie Commented Jul 5, 2020 at 4:11$DBPresults
incorrectly. Based on your query it's an array of names, but you're trying to print a single value. If you want the number of results you need to usecount( $DBPresults )
. – Jacob Peattie Commented Jul 6, 2020 at 8:17