admin管理员组

文章数量:1122826

I'm currently trying to run this php code snippet in a WordPress Page using a PHP code snippet extension.

//Database connection script
//Connecting to our master database
 global $wpdb;
 $hostname = "host";
 $username = "user";
 $password = "pass";
 $dbname = "dbname";

 //creating new database object
 $dbconnection = new $wpdb($username, $password, $dbname, $hostname );
 
 //Querying the database
 $dbconnection->show_errors( true );
 $dbQuery = $dbconnection ->get_results("SELECT * FROM Master");
 $dbconnection->last_query;
 $dbconnection->print_error();
 var_dump($dbconnection->last_query);

The output on the page looks like this, the table headers are there because of html code. The goal is to eventually get the data properly formatted under them but for now I'm getting no results from the database. The database is a separate database from the automatically generated WordPress one created through PhPMyAdmin, so the only thing I can think of is that my new wpdb object might be wrong but I made sure to double/triple check the information. I've been stuck on this for hours because I'm getting no errors either through print_error.

I'm currently trying to run this php code snippet in a WordPress Page using a PHP code snippet extension.

//Database connection script
//Connecting to our master database
 global $wpdb;
 $hostname = "host";
 $username = "user";
 $password = "pass";
 $dbname = "dbname";

 //creating new database object
 $dbconnection = new $wpdb($username, $password, $dbname, $hostname );
 
 //Querying the database
 $dbconnection->show_errors( true );
 $dbQuery = $dbconnection ->get_results("SELECT * FROM Master");
 $dbconnection->last_query;
 $dbconnection->print_error();
 var_dump($dbconnection->last_query);

The output on the page looks like this, the table headers are there because of html code. The goal is to eventually get the data properly formatted under them but for now I'm getting no results from the database. The database is a separate database from the automatically generated WordPress one created through PhPMyAdmin, so the only thing I can think of is that my new wpdb object might be wrong but I made sure to double/triple check the information. I've been stuck on this for hours because I'm getting no errors either through print_error.

Share Improve this question asked Mar 22, 2022 at 6:07 Francis BrideauFrancis Brideau 11 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0

Either you have a typo or your syntax is incorrect for this line:

$dbconnection = new $wpdb($username, $password, $dbname, $hostname );

It should be:

$dbconnection = new wpdb($username, $password, $dbname, $hostname ); (no $ in referencing the wpdb class)

If you're using the $wpdb class, you don't need to create a new connection. Just use the existing connection and create a new object. $myObj = $wpdb->some_function(args)

本文标签: Using PHP Code Snippets to query a databasedbconnectiongtgetresults is outputting nothing