As Ben suggested, you need to pass the connection details when creating the wpdb class:
$newdb = new wpdb( 'user', 'password', 'database', 'hostname' );
You should also test that the query actually returned something before using the result in a foreach loop:
if ($rows) {
foreach ($rows as $obj) {
...
}
}
Related Posts:
- WPDB Insert or if exists Update
- Inserting Post Meta From SQL
- WPDB: how to get the value of a field in a custom database table
- How to execute mulitple statement SQL queries using $wpdb->query?
- How to query the WordPress database to get posts of a certain custom post type, taxonomy and field?
- wpdb_prepare with multiple or condition
- Create table from array with prepare
- WPDB SQL query with prepare() returning variable, not db value
- Clear Terms from Taxonomy for Specific Post IDs?
- How to delete a particular row in a database table
- Getting value from database table depending on field value
- Help posting values to DB on submit using $wpdb->query
- 2 $wpdb queries causing error Table ‘wp_postmeta’ is specified twice, both as a target for ‘UPDATE’
- Correct and secure way to access a custom SQL database in a custom PHP template file
- How to get specific table by current user login
- Querying multiple meta_keys in WordPress SQL query
- Oracle SQL query for Date format
- Using wpdb to connect to a separate database
- get_results using wpdb
- $wpdb won’t insert NULL into table column
- wpdb->insert multiple record at once
- How to define composite keys with dbDelta()
- Hook into $wpdb
- How does $wpdb handle COUNT(*)
- WordPress database error: [Query was empty] [closed]
- How to auto login user again after change user_login
- Connecting to external oracle database
- Bulk Update of Custom Post Type Post Status through SQL
- How do I add indexes to WordPress Database?
- Can’t save ajax value to database
- Updating all rows of table with $wpdb
- Block search SQL from happening
- Is $wpdb->prepare escaping to much? How to use it properly?
- When and why should I use $wpdb different return types?
- How wordpress accesses its own database
- Using table lock with wp_insert_post?
- Remove database entries where post_date > expiration date
- How To Get Some Data From WordPress Database Using WordPress $wpdb Query?
- How to use a different database to list and manage comments in the backend
- Show last modified date of database
- $wpdb->insert is not working
- Extracting the post_id via the wp_insert_post action (external db query)
- In what part of the WordPress core does the users table and usermeta table get joined?
- Get stock by custom meta field on all Woocommerce variable products
- WordPress and MySQL: how to transfer Meta_key and Meta_Value from one post_id to another
- Transferring working local PHP site to wordpress – with database (MySQL)
- How do I get the posts within a certain year/date(with sql query)?
- Display fields as values in array from external SQL DB
- Getting id of poll from WP database
- I need to get an array from the wordpress database
- How to insert a single row to WP database table?
- Update from 4.5 to 4.6 failing
- Setup private content for specific users with daily updates [closed]
- $wpdb->insert Giving duplicates
- Which data is written and stored in the database when read-only (non-posting, non-commenting) users visit the site?
- Have working sql query… trying to adjust it to use $wpdb
- $wpdb->query can’t insert data
- Is it possible to have more than 1 MySQL database in my WordPress blog?
- Best practice to import user base (subscribers) from one website to another?
- AJAX wp-mysql running too slow
- Use $wpdb or other PHP script method to find/replace in WP database
- Problem migrating to localhost
- Is my way to change WordPress server is correct?
- wpdb Cannot Update column in Database
- Delete/Replace words from X to Y in post content
- PHP -> SQL Query with Summing
- SQL query to set posts in bulk based on the post content
- How to create more than one new wpdb object?
- WPDB – Read and write value from / to database
- How to Generate a list of Most Commented post?
- wpdb->insert not inserting first variable
- custom tables in wordpress Database? (can i just create them with sql?)
- WordPress SQL Prepare
- Accidently changed the GUID
- How to escape percentage sign(%) in sql query with $wpdb->prepare?
- DB prefix not updating
- Job and Employee Performance Tracking with Product Safety
- Using wpdb to connect to a different database is not working
- Is there a way for two deferent themes to consume two different DB on a same WP instance?
- How to add download link from database on custom page template?
- insert data from a form:: Warning Empty query mysqli::query()
- How can I get $wpdb to show MySQL warnings?
- About wp database hooks (error establishing connection)
- $wpdb not working
- Accessing content from third party as native posts in WordPress
- Should I Use only wpdb Class to Write Custom Queries?
- Include post_status check within $wpdb query
- Accessing external database: ERR_INCOMPLETE_CHUNKED_ENCODING
- Can not manually create a database ( db, user, pass ) for a plugin
- using same mysql user with many databases
- MySQL database migration to WordPress
- Insert Extra fields added in the front end registration form to DB
- Migrated to Namecheap, now Trouble with Database & wp-config [closed]
- Insert into db with foreach problem
- SQL query to rewrite all media URLs to end with .webp
- How to get data (not value or name) from radio options to POST to database
- Weird WP -Cli Error Connection Refused
- Retrieve data from external database and insert back in
- Uploading to WordPress Database
- Putting form result in my database