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
  • The first error, with pm_migration_start_2_3 seems to be completely unrelated to your code. What is the structure of the wp_erp_acct_products table? – Jacob Peattie Commented Jul 5, 2020 at 4:11
  • imgur/a/bilMxfF – Christoforos Kotsios Commented Jul 6, 2020 at 6:38
  • 1 Is there even an error? After formatting the messages you'd included in the question there doesn't appear to be any errors. You appear to be just using $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 use count( $DBPresults ). – Jacob Peattie Commented Jul 6, 2020 at 8:17
Add a comment  | 

1 Answer 1

Reset to default 0

It 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