Home>

Bored, it ’s technical, but it ’s not easy to write in the background.Just want to read the data in wordpress directly.When doing ajax reading background data, I randomly searched a piece of code on the Internet and used wordpress $wpdb to read the mysql database.I found other people's end-to-end codes and started my journey of correcting mistakes.

The main issues are:

1, non-object

2, Chinese characters in json,/is transcoded

3, chrome can access php files normally, ie shows http 404, ajax shows 404 in the header, but the response body is correct,Is the json I want.

First paste the code found online:

<?php
global $wpdb;
$a=$wpdb->get_results ("select id, post_title from wp_posts");
echo json_encode ($a);
?>

At this time, an error will be reported,non-object;

Some people on the Internet say that you can add require_once ("wp-blog-header.php");then congratulations on your winning,You will find that chrome can access the php file normally, ie shows http 404, ajax shows 404 in the header, but the response body is correct.

After modifying it to require_once ("wp-config.php");

The json obtained at this time is the echo json_encode ($a);

Modify it to echo str_replace ("\\/", "/", json_encode ($a, json_unescaped_unicode));the result is correct

Final Results:

<?php
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_relationship_id_term.term.terms_term "";
echo str_replace ("\\ /", "/", json_encode ($a, json_unescaped_unicode));
?>
  • Previous jQuery controls the DIV layer to achieve the effect of changing from large to small and from far to near animation
  • Next Detailed ASP encoding and decoding functions