Problems and Solutions when using wordpress's $ wpdb class to read mysql databases for ajax

Source: Internet
Author: User
: 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.