WordPress.org

Support

Support » Plugins and Hacks » Hacks » [Resolved] WordPress get_results from custom table

[Resolved] WordPress get_results 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)
  • 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>";
    	}

    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
    		"
    	);

    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 ‘[Resolved] WordPress get_results from custom table’ is closed to new replies.
Skip to toolbar