admin管理员组文章数量:1123506
I'm trying to select the post_title
from wp_posts
with a query which I did using this method:
global $wpdb;
$test = $wpdb->get_var(
$wpdb->prepare(" SELECT post_title FROM {$wpdb->wp_posts}" )
);
echo "$test";
EDIT: It now gives me:
Notice: Undefined property: wpdb::$wp_posts in /../../../../wp-includes/wp-db.php on line 566
Warning: Missing argument 2 for wpdb::prepare(), called in /../../../../wp-content/themes/pms72/page-home.php on line 73 and defined in /../../../../wp-includes/wp-db.php on line 992
I'm trying to select the post_title
from wp_posts
with a query which I did using this method:
global $wpdb;
$test = $wpdb->get_var(
$wpdb->prepare(" SELECT post_title FROM {$wpdb->wp_posts}" )
);
echo "$test";
EDIT: It now gives me:
Notice: Undefined property: wpdb::$wp_posts in /../../../../wp-includes/wp-db.php on line 566
Warning: Missing argument 2 for wpdb::prepare(), called in /../../../../wp-content/themes/pms72/page-home.php on line 73 and defined in /../../../../wp-includes/wp-db.php on line 992
Share
Improve this question
edited Mar 3, 2014 at 16:20
David H
asked Mar 3, 2014 at 16:00
David HDavid H
7612 gold badges7 silver badges16 bronze badges
4
|
3 Answers
Reset to default 1First, it's not $wpdb->wp_posts
, it's just $wpdb->posts
.
Second, you're using prepare() incorrectly. You use prepare when you have some variable data that you need to safely insert into the query string, not otherwise. Using prepare on a known string does nothing and throws a warning.
For example:
$data = 'example string'
$query = $wpdb->prepare("SELECT post_title FROM {$wpdb->posts} where post_title = %s", $data);
And then $query will be the SQL string that you're able to safely send to $wpdb->query. The string gets escaped, quoted, and inserted where that %s is.
If you don't have any variable data that needs to be prepare'd for inserting into the query, then there's no point in calling prepare.
For your case, since you're not putting any variables into the SQL, just skip the prepare.
$test = $wpdb->get_var( "SELECT post_title FROM {$wpdb->posts}" );
You should be able to get the posts
table name using.
$tableName = $wpdb->prefix . 'posts';
So, your script would become:
global $wpdb;
$tableName = $wpdb->prefix . 'posts';
$test = $wpdb->get_var(
$wpdb->prepare(" SELECT post_title FROM {$tableName}" )
);
var_dump($test);
if ($row>0) {
session_start();
$_SESSION['admin_id']= 'admin_id';
$_SESSION['username']= 'username';
header("location:http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/admin.php");
}
I get an error when I run the localhost, saying undefined variable $row
.
本文标签: phpUndefined variable row
版权声明:本文标题:php - Undefined variable: row? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736575162a1944840.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
global $wpdb
right? – David H Commented Mar 3, 2014 at 16:19