简体   繁体   中英

Cannot get data from table of WordPress MYSQL

I got a table called newdata and there is a column called name . I'm trying to get the value of name where the ID is 1 with the following code:

<?PHP
$table_name = $wpdb->prefix . 'newdata';
$results = $wpdb->get_col($wpdb->prepare("SELECT name FROM $table_name WHERE id = 1"));
?>

But the error shows:

Undefined variable: wpdb and Trying to get property 'prefix' of non-object

and

Uncaught Error: Call to a member function query()`.

Anything I do wrong?

Edited: I don't need to define the wpdb like global $wpdb; because it's not within a function.

You are trying to use WordPress variable without context, where $wpdb is defined? You haven't defined it in your script, nor used WordPress init procedure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM