: This article mainly introduces the problems and solutions when using wordpress's $ wpdb class to read mysql database for ajax. if you are interested in PHP tutorials, refer to it. I am bored with www.jiazhuangma.com. I want to directly read the data in wordpress if the technology is not well-written in the background. Do
AjaxWhen reading background data, I searched for a piece of code on the Internet and used wordpress $ wpdb to read my
SQL databaseIt's just like this. I found someone else's code and started my mistake correction journey.
Main problems:
1, non-object
2. Chinese characters in json,/transcoded
3. chrome can access the php file normally, and ie displays http 404,Ajax404 is displayed in the header, but the response body is correct, which is the json I want.
Reprint please indicate http://www.cumt.top/blog? P = 162
First paste the code found on the Internet:
get_results(“SELECT ID,post_title FROM wp_posts ″);echo json_encode($a );?>
At this time, an error is reported, non-object;
Some people say they can addRequire_ Once ('WP-blog-header.php '); so congratulations, you will find that chrome can access the php file normally, ie shows http 404,AjaxThe header displays 404, but the response body is correct.
ChangeRequire_ Once ('WP-config. php'); after normal,
In this case, after transcoding, the json file needs to echo json_encode ($ );
Modify it to echo str_replace ("\/", "/", json_encode ($ a, JSON_UNESCAPED_UNICODE). The result is correct.
Final result:
require_once(‘wp-config.php’);global $wpdb;$a = $wpdb->get_results(“SELECT wp_posts.ID,wp_posts.post_title,wp_posts.guid FROM wp_posts inner join wp_term_relationships on wp_term_relationships.object_id=wp_posts.ID where wp_term_relationships.term_tax ORDER BY wp_term_relationships.object_id DESC LIMIT 5″);echo str_replace(“\\/”, “/”, json_encode($a,JSON_UNESCAPED_UNICODE ));?>
Reprint please indicate http://www.cumt.top/blog? P = 162
The above describes the problems and solutions for using wordpress's $ wpdb class to read mysql database for ajax, including The require, ajax, object_id, sqldatabase, and inner join content, if you are interested in PHP tutorials.