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
  • 1 $row is undefined, it hasn't been given a value, so it's incorrect to try to retrieve a post title, as it's not an array of values, it's empty/null/void. Its like buying a brand new notebook then wondering why all the pages are blank. – Tom J Nowell Commented Mar 3, 2014 at 16:09
  • Yea I noticed I just forgot to change this page, it was really stupid of me but regardless I changed it up now and it's telling me $wpdb is undefined. – David H Commented Mar 3, 2014 at 16:17
  • You need to declare $wpdb as a global before using it. – Andrew Bartel Commented Mar 3, 2014 at 16:18
  • I have. global $wpdb right? – David H Commented Mar 3, 2014 at 16:19
Add a comment  | 

3 Answers 3

Reset to default 1

First, 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