WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] turn wpdb->get_results.... into a single dimensional array (2 posts)

  1. whooami
    Member
    Posted 7 years ago #

    Ive been trying with no success to turn the result of a simple MySQL query that looks at one column and returns all of the items in a column, using WordPress' built in db class, into a simple array.

    I found one other similar post that kind of helped, it changed the array, but still didnt provide the result I'm looking for.

    I'm trying to archive this simple one-dimensional array:

    Array ( [0] => cat [1] => dog )

    So far, manipulating the various wpdb query options, I've gotten this:

    Array ( [0] => Array ( [0] => cat ) [1] => Array ( [0] => dog ) )

    and,

    Array ( [0] => Array ( [id] => 14 [mysql-field] => cat ) [1] => Array ( [id] => 15 [mysql-field] => dog ) )

    The query is simple enough:

    $sql = "SELECT animals from $table_name";
    $pets = $wpdb->get_results($sql);

    Am I missing something really simple here? Or can anyone lend some advice?

  2. whooami
    Member
    Posted 7 years ago #

    for anyone interested here is the rather simple solution:

    $sql = 'select ID, blah from '.$table_name;
    		$things = array();
    		$results = $wpdb->get_results($sql );
    		foreach( $results as $result )
    		$things[] = $result->blah;

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags