0

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. enter image description here

2 Answers 2

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)

0

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)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.