Jump to content

Recommended Posts

Hello. I'm new here, forgive me if this is the wrong place. I need some assistance with this script:

 

I'm getting: Fatal error: Call to a member function fetch_object() on a non-object in /home/transtar/public_html/blog/word.php on line 25

 

<?php
    //MySQL Username
    $user = "";
    //MySQL Password
    $pass = "";
    //MySQL Database Name
    $database = "";
    //Number of posts you want to have appear
    $numOfPosts = 7;

    //Setup connection
    $mysqli = new mysqli("localhost", $user, $pass, $database);

    /* check connection */
    if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
    }

    $limit = $numOfPosts;

    $sql = "SELECT * FROM wp_posts WHERE post_type = 'post' AND post_status IN ( 'draft', 'publish', 'future', 'pending', 'private' ) ORDER BY post_date DESC LIMIT $limit";
    $result = $mysqli->query($sql);

    while ($row = $result->fetch_object()) {
    echo '<ul>';
    echo '<li><a href="'.$row->guid.'">'.$row->post_title.'</a></li>';
    echo '</ul>';
    }?>

 

Thank you so much

Link to comment
https://forums.phpfreaks.com/topic/206791-php-code-help/
Share on other sites

The error message is fairly self explanatory, $result->fetch_object() is on a non-object. Therefore, $result is a non-object. Since $result is being set in your code by $result = $mysqli->query($sql), doesn't that suggest that the code failed to return an object and you would need to debug why a call to the ->query() function was failing?

 

If you echo $mysqli->error it will tell you why your query is failing.

Link to comment
https://forums.phpfreaks.com/topic/206791-php-code-help/#findComment-1081444
Share on other sites

The error message is fairly self explanatory, $result->fetch_object() is on a non-object. Therefore, $result is a non-object. Since $result is being set in your code by $result = $mysqli->query($sql), doesn't that suggest that the code failed to return an object and you would need to debug why a call to the ->query() function was failing?

 

If you echo $mysqli->error it will tell you why your query is failing.

 

Appreciate that. It simply pulls the last 7 posts from Wordpress and display them. I don't suppose anyone is kind enough to help?

Link to comment
https://forums.phpfreaks.com/topic/206791-php-code-help/#findComment-1081448
Share on other sites

$sql = "SELECT * FROM wp_posts WHERE post_type = 'post' AND post_status IN ( 'draft', 'publish', 'future', 'pending', 'private' ) ORDER BY post_date DESC LIMIT $limit";
if(!$result = $mysqli->query($sql)) {
die("Error in $sql <br />" . $mysqli->error);
}

Link to comment
https://forums.phpfreaks.com/topic/206791-php-code-help/#findComment-1081455
Share on other sites

$sql = "SELECT * FROM wp_posts WHERE post_type = 'post' AND post_status IN ( 'draft', 'publish', 'future', 'pending', 'private' ) ORDER BY post_date DESC LIMIT $limit";
if(!$result = $mysqli->query($sql)) {
die("Error in $sql <br />" . $mysqli->error);
}

 

Thank you:

 

However: Error in SELECT * FROM wp_posts WHERE post_type = 'post' AND post_status IN ( 'draft', 'publish', 'future', 'pending', 'private' ) ORDER BY post_date DESC LIMIT 7

Table 'transtar_07LhCwo.wp_posts' doesn't exist

Link to comment
https://forums.phpfreaks.com/topic/206791-php-code-help/#findComment-1081457
Share on other sites

Seriously, that error message is blatantly self explanatory. Did you bother to read it  -

Table 'transtar_07LhCwo.wp_posts' doesn't exist

 

In your database: transtar_07LhCwo, there is no table wp_posts. You either have the wrong database selected or the table wp_posts does not exist with that spelling and capitalization (assuming you are on an operating system that is case-sensitive.)

Link to comment
https://forums.phpfreaks.com/topic/206791-php-code-help/#findComment-1081462
Share on other sites

Seriously, that error message is blatantly self explanatory. Did you bother to read it  -

Table 'transtar_07LhCwo.wp_posts' doesn't exist

 

In your database: transtar_07LhCwo, there is no table wp_posts. You either have the wrong database selected or the table wp_posts does not exist with that spelling and capitalization (assuming you are on an operating system that is case-sensitive.)

 

Thanks - works a treat now!

Link to comment
https://forums.phpfreaks.com/topic/206791-php-code-help/#findComment-1081464
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.