$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>WordPress Database Query works in phpMyAdmin but not in the code|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

WordPress Database Query works in phpMyAdmin but not in the code

matteradmin10PV0评论

I have a simple SQL query: SELECT * FROM test_table__csv_import_temp_joblist WHERE url =''

In phpMyAdmin, it works well and returns a result. In WP it returns nothing:

$sql        = "SELECT * FROM test_table__csv_import_temp_joblist WHERE url =''";
$res        = $wpdb->query( $sql );

What is the issue with my query?

$wpdb->last_query returns the right query. $wpdb->last_error returns nothing.

Many Thanks

I have a simple SQL query: SELECT * FROM test_table__csv_import_temp_joblist WHERE url ='https://www.example'

In phpMyAdmin, it works well and returns a result. In WP it returns nothing:

$sql        = "SELECT * FROM test_table__csv_import_temp_joblist WHERE url ='https://www.example'";
$res        = $wpdb->query( $sql );

What is the issue with my query?

$wpdb->last_query returns the right query. $wpdb->last_error returns nothing.

Many Thanks

Share Improve this question asked Jan 15, 2019 at 15:14 user998163user998163 3137 silver badges15 bronze badges 1
  • Are you certain that this table is in the same database as the one WordPress is using? – Jacob Peattie Commented Jan 15, 2019 at 15:26
Add a comment  | 

1 Answer 1

Reset to default 0

Your problem is that you use query method of wpdb and if you read it’s docs:

The query function allows you to execute any SQL query on the WordPress database. It is best used when there is a need for specific, custom, or otherwise complex SQL queries. For more basic queries, such as selecting information from a table, see the other wpdb functions above such as get_results, get_var, get_row or get_col.

query  (string) The SQL query you wish to execute. This function returns an integer value indicating the number of rows affected/selected for SELECT, INSERT, DELETE, UPDATE, etc. For CREATE, ALTER, TRUNCATE and DROP SQL statements, (which affect whole tables instead of specific rows) this function returns TRUE on success. If a MySQL error is encountered, the function will return FALSE. Note that since both 0 and FALSE may be returned for row queries, you should be careful when checking the return value. Use the identity operator (===) to check for errors (e.g., false === $result), and whether any rows were affected (e.g., 0 === $result).

You’ll see that it’s not what you want.

If you want to get results of the query, you should use get_results method:

$res = $wpdb->get_results( $sql );

Post a comment

comment list (0)

  1. No comments so far