Forums

[resolved] WordPress get_results from custom table (3 posts)

  1. yochi
    Member
    Posted 4 months ago #

    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?

  2. shansta
    Member
    Posted 4 months ago #

    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>";
    	}
  3. yochi
    Member
    Posted 4 months ago #

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

Reply

You must log in to post.

About this Topic