1

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.

2
  • Do you mean that each Name in each row should be an <option> in a <select> html tag? Commented Mar 29, 2016 at 16:29
  • @dewd yes so up there it says jesus,james,matt i need it to show up as options the code below dose nto work since im using shortcodes[insertphp][/insertphp] Commented Mar 29, 2016 at 17:20

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

i should of mentioned this before im using [insertphp][/insertphp] short code i took out the <?php ?> and the code dose not see to work
i am not familiar with the [insertphp] plugin... if it's an issue with the plugin, maybe try changing the : for { and the endforeach for a {, though I don't think that's the problem. Is there any way you can test this in raw php?
@jamespotter1 see my edit above, maybe that will help with the plugin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.