jack_of_all_trades
jack_of_all_trades

Reputation: 332

How do I write this with mysqli?

Trying to convert mysql to mysqli. I just got somewhat comfortable using queries, but I'm not sure exactly what I need to do now. Here is what's going on:

Access the "wp_users" table (wouldn't be surprised if I wrote this wrong):

mysqli_query($conn, "SELECT * as `value` FROM `wp_users` WHERE 1");
while ($row = mysqli_fetch_assoc($query)) {
    $result_wp_users = $row[value];
}

Can't find the users:

if ( !$result_wp_users) {
    die('</br> Cannot find any users:' . mysqli_error($conn));
}

I need to get this information (but using mysqli):

$display_name = mysql_result($result_wp_users, $i, 'display_name');
echo $display_name;

I've tried writing this a few different ways with mysqli, but I can't get it to work. Any help?

Also, anyone have a good resource for getting comfortable with mysqli? The API is giving me more confusion at this point (I'm extremely new to all this). I really need it explained to me like I'm five years old.

Upvotes: 1

Views: 53

Answers (1)

TeeDeJee
TeeDeJee

Reputation: 3741

It looks like you are using wordpress. You can use the WP_User_Query class to query users.

Don't add any arguments to the WP_User_Query to get all users.

$user_query = new WP_User_Query();

// User Loop
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        echo '<p>' . $user->display_name . '</p>';
    }
} else {
    echo 'No users found.';
}

Or mysqli:

mysqli_query($conn, "SELECT * FROM wp_users WHERE 1 = 1");

Upvotes: 1

Related Questions