简体   繁体   English

Wordpress MySQL查询失败

[英]Wordpress MySQL query fails

$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 谢谢

re. 回覆。 your comment: Call to undefined function  die() 您的评论:调用未定义的函数die()

It looks like you might have some non-ASCII character in the whitespace before your die() statement. 看起来您的die()语句之前,您的空格中可能包含一些非ASCII字符。 Try deleting that whitespace and reinserting it, and maybe you'll find out what the database error is 尝试删除该空格并重新插入它,也许您会发现数据库错误是什么

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... 应该使用wpdb类与数据库进行通信...

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'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM