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 badge2 Answers
Reset to default 0Either 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
版权声明:本文标题:Using PHP Code Snippets to query a database, $dbconnection->get_results is outputting nothing 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736294792a1929433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论