• kyuknis

    (@kyuknis)


    Greetings,

    I’m trying to make a go-between for a WordPress site and a set of mobile apps for a client. The code I’ve been using so far (very early work in progress) has already encountered an issue. When I query the wp_posts table, it only returns part of the contents of the post in the post_contents column. I was wondering if anyone could offer any insight as to why this is?

    Example of returned content vs actual content:
    Returns:
    “Pastor Jeff suggests in this talk specific things that we can do as believers each day. If you aim at nothing, you will hit it every time. We have to plan to be?intentional. “

    Actual content from website:
    “Pastor Jeff suggests in this talk specific things that we can do as believers each day. If you aim at nothing, you will hit it every time. We have to plan to be intentional.

    One of the things that he suggests on our journey toward intentionality is “TEXT TEN Thursday” This is intentional. It will require you to spend some time and thought. We want you to text ten of your friends every Thursday.

    Send them a brief text letting them know that you are thinking about and praying for them. Being intentional means to encourage, to exhort and edify. You should make each text to each friend different. Do not be lazy and copy and paste…make the text special and from your heart.”

    The Query:
    SELECT * FROM DbName.wp_posts ORDER BY post_date_gmt DESC;

    The PHP code:

    <?php
    
        // Create defintions for login data
        // The name of the database for WordPress
        define('DB_NAME', 'TheDatabasesName');
    
        // MySQL database username
        define('DB_USER', 'TheUserName');
    
        // MySQL database password
        define('DB_PASSWORD', 'ThePassword');
    
        // MySQL hostname
        define('DB_HOST', 'TheHostName');
    
        // Create connection
        $con=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    
        // Determine if connection was successful
        if (mysqli_connect_errno())
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    
        // Get all of the data from the wp_posts table and order it by the date it was posted
        // DESC by post_date_gmt is for simpler interoperability with NSDate object from Foundation
        $sql = "SELECT * FROM DbName.wp_posts ORDER BY post_date_gmt DESC;";
    
        // Check if there are results
        if ($result = mysqli_query($con, $sql))
        {
            // Results array
            $resultArray = array();
            // Temporary array
            $tempArray = array();
    
            // Process for each row in the result set
            while($row = $result->fetch_object())
            {
                // Adds the row to the results array
                $tempArray = $row;
                array_push($resultArray, $tempArray);
            }
    
            // Finally, encode the array and output the results in JSON format
            echo json_encode($resultArray);
            }
    
        // Close connections
        mysqli_close($result);
        mysqli_close($con);
    ?>

    The only thing I can think of at the moment is that post_content is only a segment of the post meant to be displayed on other parts of the website and the actual content is in another part of the database that I can not find. Thank you in advance for any assistance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • catacaustic

    (@catacaustic)

    What I believe is happening there has to do with the way that you’re trying to work with the content and the format that you’re trying to use it as.

    If my memory serves me right, JSON doesn’t allow new lines in it’s content, as they break the content flow. that’s what you’re seeing there. It cuts off at the new line.

    There are ways to get around this, but the most cross-platform one that you can use is to use base44 encoding as this will encode everything correctly when you send it through.

    One other thing that you should remember as well is that by getting the content by the SQL query like that, you won’t be getting most of the formatting, as that’s added by WordPress after the content is retrieved, so if you want it to look the same… it won’t.

    Thread Starter kyuknis

    (@kyuknis)

    Thank you for the information! I actually just ran a curl command in terminal and found out that it actually works fine, it was MySQL Workbench that was having issues. As far as the formatting, I’m not to worried about it because I’ve already made a custom class to handle that on the end users device (the server is poor in performance, best not to put too much more strain on it). Thank you so much for the reply!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Whole Post Wont Show When Db Queried From PHP’ is closed to new replies.