Title: WordPress get_results from custom table
Last modified: August 20, 2016

---

# WordPress get_results from custom table

 *  Resolved [yochi](https://wordpress.org/support/users/yochi/)
 * (@yochi)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/wordpress-get_col-from-custom-table/)
 * Hi
 * I am working on a plugin. It is my first one where i create a new table. I have
   got the creating part working.
 * In the settings-section i have a select box i want to fill with places from the
   table.
 * The code i have now is taken from the codex:
 *     ```
       global $wpdb;
   
       	$fivesdrafts = $wpdb->get_results(
       		"
       		SELECT name
       		FROM $wpdb->testdatabase
       		", OBJECT
       	);
   
       	foreach ( $fivesdrafts as $fivesdraft )
       	{
       		echo "<option>". $fivesdraft . "</option>";
       	}
       ```
   
 * When i check the page the select-box is completly empty. I have tried to fill
   the select-box with hard coded text and it worked, so my function is working.
 * Anyone know what the problem is?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [shansta](https://wordpress.org/support/users/shansta/)
 * (@shansta)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/wordpress-get_col-from-custom-table/#post-2494705)
 * try
 *     ```
       $wp_query->request =
       		"
       		SELECT name
       		FROM $wpdb->testdatabase
       		";
               $fivesdraft = $wpdb->get_results($wp_query->request, OBJECT);
   
       	foreach ( $fivesdrafts as $fivesdraft )
       	{
       		echo "<option>". $fivesdraft . "</option>";
       	}
       ```
   
 *  Thread Starter [yochi](https://wordpress.org/support/users/yochi/)
 * (@yochi)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/wordpress-get_col-from-custom-table/#post-2494709)
 * thanks for the reply, but i had to type the full name of the table, my mistake
   was that i thought that WordPress added the prefix automaticly.
 * Here is my working code:
 *     ```
       $fivesdrafts = $wpdb->get_col(
       		"
       		SELECT name
       		FROM wp_testdatabase
       		"
       	);
       ```
   
 *  [Ryan Sutana](https://wordpress.org/support/users/ryscript/)
 * (@ryscript)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/wordpress-get_col-from-custom-table/#post-2495005)
 * Your code above could not work in other table prefix like wps_, s12_ etc.
 * Here’s the code below that works in all table prefix 😉
 *     ```
       $row_object = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}testdatabase" ) );
       ```
   
 * Hope that helps.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘WordPress get_results from custom table’ is closed to new replies.

## Tags

 * [database](https://wordpress.org/support/topic-tag/database/)
 * [wpdb](https://wordpress.org/support/topic-tag/wpdb/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 3 participants
 * Last reply from: [Ryan Sutana](https://wordpress.org/support/users/ryscript/)
 * Last activity: [13 years, 8 months ago](https://wordpress.org/support/topic/wordpress-get_col-from-custom-table/#post-2495005)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
