1
$query2 = "SELECT * FROM wp_posts WHERE post_status = 'publish'";
$result2 = mysql_query($query2);
if (!$result2) {
  die('Invalid query: ' . mysql_error());
}

if (mysql_numrows($result2) == 0) {
    echo("zero");
} else {
    echo(mysql_numrows($result2));
}

.. spent an hour on this, it should work but it doesn't, not sure where I'm going wrong.

Thanks

7
  • Should be using wpdb class to communicate with the database... codex.wordpress.org/Class_Reference/wpdb ,plus your query should work with the standard wp_query - codex.wordpress.org/Class_Reference/WP_Query Commented May 13, 2012 at 19:33
  • No, this isn't inside wordpress, using it externally for another purpose. Commented May 13, 2012 at 19:34
  • 2
    "It doesn't work" is very ambiguous. How doesn't it work? And querying SELECT COUNT(*) AS counter would be more efficient than this. Commented May 13, 2012 at 19:34
  • did you get any error message ? Commented May 13, 2012 at 19:38
  • Should probably state that in the question to avoid confusion. Commented May 13, 2012 at 19:39

3 Answers 3

1

re. your comment: Call to undefined function  die()

It looks like you might have some non-ASCII character in the whitespace before your die() statement. Try deleting that whitespace and reinserting it, and maybe you'll find out what the database error is

Sign up to request clarification or add additional context in comments.

Comments

0

You should use query like this

 $querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish'";
 $pageposts = $wpdb->get_results($querystr, OBJECT);

Should be using wpdb class to communicate with the database...

Comments

0

Try this:

$query2 = "SELECT * FROM wp_posts WHERE post_status = 'publish'";
$result2 = mysql_query("$query2");

Double quotes in the query.

Without the double quotes in the query, the query will look like:

$result2 = mysql_query(SELECT * FROM wp_posts WHERE post_status = 'publish');

Instead of:

$result2 = mysql_query("SELECT * FROM wp_posts WHERE post_status = 'publish'");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.