WordPress.org

Forums

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

  1. yochi
    Member
    Posted 3 years 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 3 years 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 3 years 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
    		"
    	);
  4. ryscript
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic