• Hi,

    iam trying to gett all users with first, last name and the mobile/phone numbers. My sql query gives that all back but in thois form:

    firstname
    firstname
    firstname
    firstname

    lastname
    lastname
    lastname
    lastname

    Phonenumber
    Phonenumber
    Phonenumber
    Phonenumber

    I want: firstname, lastname, phonenumber

    Importand: Not each user has a number.

    Can Someone help? My query:
    {

    global $wpdb;

    $test1 = $wpdb->get_col(“SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = ‘first_name'” );
    $test2 = $wpdb->get_col(“SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = ‘last_name'” );
    $test3 = $wpdb->get_col(“SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = ‘next_ad_int_mobile'” );
    $test4 = $wpdb->get_col(“SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = ‘next_ad_int_phone'” );

    $gefunden = $wpdb->num_rows;
    foreach ($test1 as $fn)
    {
    print_r($fn);
    echo “<br>”;
    }
    foreach ($test2 as $fn2)
    {
    print_r($fn2);
    echo “<br>”;
    }
    foreach ($test3 as $fn3)
    {
    print_r($fn3);
    echo “<br>”;
    }

    foreach ($test4 as $fn4)
    {
    print_r($fn4);
    echo “<br>”;
    }

    echo “Gefundene Telefonnummern: “.$gefunden;
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    I think you might want to to try to get the user meta via https://codex.wordpress.org/Function_Reference/get_user_meta

    Then you can put the meta data in the order that you want it to display.

    Thread Starter hossmann

    (@hossmann)

    This?

    <?php get_user_meta($user_id, $key, $single); ?>

    Ive tried it allready, but i dont want to get only one user_id, i need all at one time.

    Moderator bcworkz

    (@bcworkz)

    Because any user may not have one of the fields, you must assume the returns are out of sync (for example: item 20 in one list may belong to a different user than item 20 in another list). Thus you need to also get the user ID along with the meta value so output can be coordinated so each row relates to the same user. Of course, get_col() will not work for multiple fields, you should use query(). Note that query() does not return results, your result will be in $wpdb->result.

    Ideally, one of the values will have an item for every single user, such as lastname. Otherwise the data must be pre-processed prior to output. Loop through the results of that meta key. Get the corresponding entry for other key values based on the ID. With all data collected for a single ID, generate the output. Continue to the next loop iteration.

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

The topic ‘sort sql query from metadata’ is closed to new replies.