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
- Update results, Before deleting the related category [closed]
- WordPress Database Huge wp_usermeta Table
- How WordPress reacts to a lack of memory of the server [closed]
- Update from 4.5 to 4.6 failing
- Does WordPress $wpdb functions wait when table is locked?
- Setup private content for specific users with daily updates [closed]
- How Can I Put Meta_Compare in the Database-Query?
- $wpdb->insert() does not Insert record in a table
- WordPress running SQL query to update database from form
- Databases – Submitting data from inputs to database [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?
- Insert Query not working in the form
- I’m not able to get access to $wpdb [duplicate]
- $wpdb error (Call to a member function insert() on a non-object)
- Have working sql query… trying to adjust it to use $wpdb
- Problems with moving my WordPress site to another domain/server
- Is it important to have integers inserted using %d rather than %s?
- Issue running db create table query from static method
- Is it best to avoid using $wpdb for security issues?
- How to retrieve the data from the sever and displaying it in a page?
- Problem in using wpdb
- $wpdb->query can’t insert data
- Custom WordPress Table wpdb
- List of ways to access WordPress database?
- Why this query is not showing any result on wordpresss home page?
- Is it possible to have more than 1 MySQL database in my WordPress blog?
- How to fetch records from database WordPress
- Best practice to limit results in get_row()?
- pull data from a remote wordpress database
- How to migrate a database from a server to another
- Best practice to import user base (subscribers) from one website to another?
- AJAX wp-mysql running too slow
- CreateOrUpdate in WordPress
- Use $wpdb or other PHP script method to find/replace in WP database
- Problem migrating to localhost
- How can I add a new row in a separate database when someone registers via WordPress?
- Is my way to change WordPress server is correct?
- Why does changing Database Table Prefixes result in loss of content?
- I can’t update my data through $wpdb
- Trouble running $wpdb->query() with last_insert_id
- How to get a list of WordPress default database tables?
- Connecting to a different database
- wpdb Cannot Update column in Database
- Options on how to Display member specific data from a MS SQL database
- Using $wpdb | checking entered email against existing emails in db
- Delete/Replace words from X to Y in post content
- PHP -> SQL Query with Summing
- Lost in trying to create user database system
- SQL query to set posts in bulk based on the post content
- Delete database record using plugin from admin panel
- Query Column of Specific ID from Database Table
- Wpdb get->results to out the the month from the db
- Filter results from a serialized string to use on statistics
- How to create more than one new wpdb object?
- wordpress site – using custom database and PHP
- Delete all posts that do NOT have a specific tag
- Optimizing function that automatically creates internal links based on post title string
- Location of WP default mySQL tables creation file?
- Update multiple rows in one query
- How to connect and insert data in database of wordpress?
- WPDB – Read and write value from / to database
- Why my query does not run with prepare
- How to Generate a list of Most Commented post?
- Get results from wordpress data custom table
- Should I use an additional column in the DB?
- Outputting query results
- wpdb getting avatars and author url
- $wpdb->get_var – What value is returned when zero records are found?
- Add row to custom database Table and delete all rows older than 1 day
- How to display data from db in select list [closed]
- access JSON results from wordpress database with wpdb
- WordPress SQL – How to Check for Category?
- select a single val though a table in wordpress
- add_post_meta — not working
- Select From wpdb – Author/User Directory page
- WordPress SQL JOIN query
- Creating and Using Tables in the WordPress Database
- wpdb->insert not inserting first variable
- Fetching review value using wpdb class
- How do I have a user upload a blog post and then retrieve that to display in a card on the site?
- custom tables in wordpress Database? (can i just create them with sql?)
- Add posts to custom table in database instead of wp_posts
- Database SQL query error