Unable to Retrieve Data from Posts Table
-
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" . '';
-
Hi,
Make sure that you have assigned required permissions to the database and you have a data on the table..
https://codex.www.ads-software.com/Database_Description
Thanks,
Shane G.
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.
Try…
$querystr = 'SELECT * FROM '.$wpdb->prefix.'posts'; $query = $wpdb->get_results($querystr);
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.
Can i see more of the code you’re using?
You can star (*asterisk) out any passwords or personal information if necessary…
<?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;}
?>
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?
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 stringquerystr 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;} ?>
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..
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…
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.phpif ( !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” . ‘
‘;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.
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?
I’m not sure why you’re including additional files..
are you asking the OP or me?? ??
I’m not sure…. :-s …
That’s why i asked if i’m missing something… ??
- The topic ‘Unable to Retrieve Data from Posts Table’ is closed to new replies.