I am using a sql query to access information to display on a wordpress page by adding the below code to a page template. I know the query works as I've tested it in phpmyadmin.

 $rows = $newdb->get_results("SELECT TrainerName FROM trainers");   
 echo "<ul>";
 foreach ($rows as $obj) :
 echo "<li>".$obj->Name."</li>";
 echo "</ul>";

and I added this is my functions.php file $newdb = new wpdb(); $newdb->show_errors();

I can't get it to work though, I get an error "Warning: Invalid argument supplied for foreach() ..."

  • I forgot to mention I have included the database login details I just omitted them above so it wasn't shown to the whole world. ('user', 'password', 'database', 'hostname') – stemie Oct 2 '12 at 7:48

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) {
  • I tested the actual sql query in phpmyadmin to see if it works and it did. Is that what you mean when you say "You should also test that the query actually returned something before using the result in a foreach loop" – stemie Oct 2 '12 at 8:04
  • What he meant was you should have the if($rows)... part to check if the query is returning results, before starting your foreach loop. Have you got this working yet? If so, please mark the most helpful answer as accepted. If not, what are you stuck on now? – Ben HartLenn Oct 3 '12 at 1:36
  • The query may run fine in phpmyadmin, but if your wpdb class isn't connected to the right database it wont find that table and won't return any results. You can test this by printing $rows to see what's actually being returned by the wpdb query. – WhiskerSandwich Oct 3 '12 at 20:18
  • If I try the following it just returns blank 'nothing to see here' $query = "SELECT TrainerName FROM trainers"; $races= $newdb->get_results($query); if (empty($races)) { ?> <p>Nothing to see here.</p> <?php } else { ?> <table id="rr"> <tr><th>Trainer</th></tr> <?php foreach($races as $race): ?> <tr> <td><?php echo $race->TrainerName; ?></td> </tr> <?php endforeach; ?> </table> – stemie Oct 4 '12 at 15:38
  • I also meant to say, I am copy pasting the DB details straight out of my hosting control panel in the order specified by wpdb and what you guys wrote above so Im almost certain its the right db. – stemie Oct 4 '12 at 15:47

Where you have this:

$newdb = new wpdb();

You need to give the new database connection info so it can connect. Assuming you have the same user,password, and host for your new database, you could use a few of the available constants, but you will at least need to define the db name specifically:

$newdb = new wpdb(DB_USER, DB_PASSWORD, 'myNewDbName', DB_HOST);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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