Unsure if my query is wrong or my foreach loop is in the wrong place

Getting a loop through an external schema table seems like it should be pretty clear cut, but since I’m in noob territory my guess is that my query syntax is wrong?

Function:

public function tidestations() {
    global $wpdb;
    
    $tidedb = new $wpdb('example', 'example_dbpassword', 'exampledb', 'localhost');
    
    $ta = $tidedb->get_results("SELECT station FROM conditions WHERE id > 0");

    return $ta;
}

Blade:

    @foreach ($tidestations as $ts) 
		{!! $ts !!}
	@endforeach

Error:

Warning: Invalid argument supplied for foreach()

In the successfully connected schema, the conditions table has 3 columns: id, stations, waterlevel

Updated query is now displaying no results or debug messages, even though data exists in the table and each row has data in each column.

Nothing wild or unusual about the basic query or table structure.

    $ta = array();

    foreach ($tidedb->get_results("SELECT id, stations FROM conditions") as $ti) {

        $ta[] = $ti();

    }

    return $ta;

With the above, I receive the same error about the foreach.

Calling only {!! $tidestations !!} displays nothing at all. No error message, no output.

Got it.

For anyone looking to loop through external schema table rows, this is for you.

public function tidestations() {
    global $wpdb;
    
    $tidedb = new $wpdb('root', 'devpw', 'exdb', 'localhost');

    $ttable = $tidedb->get_results("SELECT * FROM conditions", ARRAY_A);

    $tstations = array();

    foreach($tidedb->get_results("SELECT * FROM conditions") as $key => $row) {
        $station = $row->station;

        $tstations[] = $row->station;
    }

    return $tstations;

}

This topic was automatically closed after 42 days. New replies are no longer allowed.