Jump to content

PHP Code Help


TranStar

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);
}

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.