Forums

[resolved] Unable to Retrieve Data from Posts Table (16 posts)

  1. maryandpaul
    Member
    Posted 2 years ago #

    I'm trying to access some data from my "posts" table and am getting NULL results. The database "wpdb" is open in my PHP code. The posts table has data.

    $mypost = $wpdb->get_results("SELECT * FROM $wpdb->posts"); 
    
    $type = gettype($mypost);
    echo '' . "mypost is $type" . '';
  2. Shane G
    Member
    Posted 2 years ago #

    Hi,

    Make sure that you have assigned required permissions to the database and you have a data on the table..

    http://codex.wordpress.org/Database_Description

    Thanks,

    Shane G.

  3. maryandpaul
    Member
    Posted 2 years ago #

    Thank you for your reply. I contacted GoDaddy and they said that there are no permissions on the database. The database table has data.

    Still in a quandry.

    Paul B.

  4. Mark / t31os
    Moderator
    Posted 2 years ago #

    Try...

    $querystr = 'SELECT * FROM '.$wpdb->prefix.'posts';
    $query = $wpdb->get_results($querystr);
  5. maryandpaul
    Member
    Posted 2 years ago #

    Thank you for your reply. I believe that my problem is that I am trying to display wordpress table data outside of the wordpress application. I successfully display the database name in the code, so I know that I am getting to the wordpress database (mysql). In the code I have an include for wp-db.php and a global $wpdb, Something is missing and I don't know what that is. My SELECT and subsequent QUERY results in a NULL return value.

  6. Mark / t31os
    Moderator
    Posted 2 years ago #

    Can i see more of the code you're using?

    You can star (*asterisk) out any passwords or personal information if necessary...

  7. maryandpaul
    Member
    Posted 2 years ago #

    <?php

    //Connect To Database

    $hostname="rec0832109095959.db.3175150.hostedresource.com";
    $username="rec0832109095959";
    $password="********";
    $dbname="$wpdb";
    $usertable="wp_posts";
    $yourfield = "post_author";
    $db = "rec0832109095959";

    $link = mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
    mysql_select_db($dbname);

    global $wpdb;
    global $type;

    include ('wp-db.php');

    $db_list = mysql_list_dbs($link);

    while ($row = mysql_fetch_object($db_list)) {
    echo $row->Database . "\n";
    }

    $querystr = 'SELECT * FROM '.$wpdb->prefix.'posts';
    $query = $wpdb->get_results($querystr);

    $type = gettype($querystr);
    echo '
    ' . "querystr is $type" . '
    ';
    echo '
    ' . "querystr is $querystr" . '
    ';

    $type = gettype($query);
    echo '
    ' . "query is $type" . '
    ';

    foreach ($query as $value) { echo $value;}

    ?>

  8. Mark / t31os
    Moderator
    Posted 2 years ago #

    This is incorrect..
    $dbname="$wpdb";

    should be..
    $dbname = $wpdb;

    Otherwise $dbname is becoming literal text $wpdb, the dollar sign is parsed as a dollar sign and not as a variable..

    Does that help?

  9. maryandpaul
    Member
    Posted 2 years ago #

    I've changed the code to $dbname=$wpdb; (code below) but I'm still getting the following results. Thank you so much for your continuing help.

    --------------------------------------------------------------------
    nformation_schema rec0832109095959
    querystr is string

    querystr is SELECT * FROM posts

    query is NULL

    Warning: Invalid argument supplied for foreach() in /home/content/m/a/r/maryandpaul/html/rt/filedata2.php5 on line 37

    <?php
    
    //Connect To Database
    
    $hostname="rec0832109095959.db.3175150.hostedresource.com";
    $username="rec0832109095959";
    $password="rbnl7Gx4";
    $dbname=$wpdb;
    $usertable="wp_posts";
    $yourfield = "post_author";
    $db = "rec0832109095959";
    
    $link = mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>
    alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
    mysql_select_db($dbname);
    
    global $wpdb;
    global $type;
    
    include ('wp-db.php'); 
    
    $db_list = mysql_list_dbs($link);
    
     while ($row = mysql_fetch_object($db_list)) {
         echo $row->Database . "\n";
    }
    
    $querystr = 'SELECT * FROM '.$wpdb->prefix.'posts';
    $query = $wpdb->get_results($querystr);
    
    $type = gettype($querystr);
    echo '' . "querystr is $type" . '';
    echo '' . "querystr is $querystr" . ''; 
    
    $type = gettype($query);
    echo '' . "query is $type" . '';
    
    foreach ($query as $value) {   echo $value;}            
    
    ?>
  10. Mark / t31os
    Moderator
    Posted 2 years ago #

    I was wrong anyway, double quotes are perfectly valid around a variable name... taht's my fault, as i'm constantly writing with single quotes and i get use to not using them with pure variable strings..

    I assume this is some page sat outside WordPress and you're just trying to grab some data from the WordPress database...

    It's been done before lots of times before, i'll dig up a few threads for you..

  11. Mark / t31os
    Moderator
    Posted 2 years ago #

    Never mind i just tested some code myself... it was dead easy...

    Include the WordPress config file instead of doing this..

    $hostname="rec0832109095959.db.3175150.hostedresource.com";
    $username="rec0832109095959";
    $password="rbnl7Gx4";
    $dbname=$wpdb;
    $usertable="wp_posts";
    $yourfield = "post_author";
    $db = "rec0832109095959";

    Just scrap that and do something like..

    <?php
    include('some/path/to/the/wordpress/install/wp-config.php');
    global $wpdb;
    
    $qstr = 'SELECT * FROM '.$wpdb->prefix.'posts';
    $query = $wpdb->get_results($qstr);
    ?>

    Of course add you own bits after that...

    That small piece of code works for me in a test file i've just created, and in honesty i wasn't expecting it to be so easy, lol...

    NOTE: $query will be an array...

  12. maryandpaul
    Member
    Posted 2 years ago #

    I included wp-settings.php but still got the error.

    Warning: require_once(/home/content/m/a/r/maryandpaul/html/rt/wp-settings.php) [function.require-once]: failed to open stream: No such file or directory in /home/content/m/a/r/maryandpaul/html/rt/wp-config.php on line 34

    Fatal error: require_once() [function.require]: Failed opening required '/home/content/m/a/r/maryandpaul/html/rt/wp-settings.php' (include_path='.:/usr/local/php5/lib/php') in /home/content/m/a/r/maryandpaul/html/rt/wp-config.php on line 34

    -----------------------------------------------------------
    wp-config.php

    if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');
    require_once(ABSPATH . 'wp-settings.php'); <== line 34

    ------------------------------------------------------------
    include('./wp-config.php');
    global $wpdb;

    $qstr = 'SELECT * FROM '.$wpdb->prefix.'posts';
    $query = $wpdb->get_results($qstr);

    $type = gettype($qstr);
    echo '
    ' . "qstr is $type" . '
    ';
    echo '
    ' . "qstr is $qstr" . '
    ';

  13. whooami
    Member
    Posted 2 years ago #

    you need to include wp-blog-header.php, or wp-load.php (for newer versions) not wp-settings.php

    if you notice your including something which is, in turn, including 'it' -- may or may not be germane to the problem, but its 'incorrect' all the same. Or at least it used to not work.

  14. Mark / t31os
    Moderator
    Posted 2 years ago #

    I only needed to include the config file in testing, as per the code i posted above....

    I'm not sure why you're including additional files..

    As i missing something here?

  15. whooami
    Member
    Posted 2 years ago #

    I'm not sure why you're including additional files..

    are you asking the OP or me?? :)

  16. Mark / t31os
    Moderator
    Posted 2 years ago #

    I'm not sure.... :-s ...

    That's why i asked if i'm missing something... :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.