Reputation: 23
I have a WordPress page that connects to an external database from WordPress using the following code:
$my_wpdb = new wpdb('me', 'password', 'database', 'localhost');
$myrows = $my_wpdb->get_results( "SELECT Name FROM testing" );
...then i use print_r($myrows);
and get the following:
Array
(
[0] => stdClass Object (
[Name] => Jesus
)
[1] => stdClass Object (
[Name] => James
)
[2] => stdClass Object (
[Name] => Matt
)
)
Now I need to output the names inside those objects in a select tag using php.
Any help would be appreciated.
Upvotes: 1
Views: 681
Reputation: 101
There you go:
<select>
<?php foreach ($myrows as $myrow) : ?>
<option value=""><?php echo $myrow->Name; ?></option>
<?php endforeach; ?>
</select>
Also, by the looks of your table it might be easier to just use WordPress get_col()
function, which works pretty much the same way as get_results
but should directly return "Name".
EDIT - Maybe the [insertphp] plugin likes this version better:
<select>
[insertphp]
foreach ($myrows as $myrow) {
echo '<option>' . $myrow->Name . '</option>';
}
[/insertphp]
</select>
Also, please provide any error info that might help troubleshoot the problem otherwise.
Upvotes: 1