I need to read/write to the wp_users table

I want to use the data in the wp_users table to display directly in pages on my site but when I try to access this in PHP all I get is a single record and not the entire list of users. I'm using exactly the same code as in a page accessing another database (not in WP) & simply changing the connection parameters (username, password etc). This code works perfectly, but when I substitute the wp_users table I just get the last record in the table. It seems like there is some special protection for the wp table? How can I overcome this please?

  • caucus
    • Site Builder, Child of Zeus

    Ari, here is my code

    <body>
    <div id="table_div">
    <?php
    date_default_timezone_set('GMT');
    $hostname = "localhost";
    $database = "db_name";
    $username = "username";
    $password = "password";
    mysql_connect($hostname, $username, $password);
    @mysql_select_db($database) or die( "Unable to select database");
    
    $query = (" SELECT * FROM wp_users");
    $result = mysql_query($query);
    while($row=mysql_fetch_array($result)){
    $results_array[$row['id']] = $row;
    }
    ?>
    <table id="rounded-corner" width="267" cellspacing="1" class="tablesorter" >
        <thead>
            <tr>
            <th width="147" nowrap="nowrap" class="rounded-company" scope="col">Player</th>
            <th width="50" nowrap="nowrap" class="rounded-q2" scope="col">email</th>
            </tr>
        </thead>
    <?php
    foreach ($results_array as $id => $row) {
    $player = $row['user_login'];
    $email = $row['user_email)'];
    ?>
    	<tr>
    	<td class="padded" nowrap="nowrap"><?php echo $player;?></td>
            <td class="padded_c" nowrap="nowrap"><?php echo $email;?></td>
    	</tr>
    <?php
    $i++;
    }
    ?>
    </table>
    </div>
    </body>
  • aristath
    • Recruit

    Hello again @caucus,
    I'm sorry for the delay on this post, I missed your last reply!

    Instead of writing your own code, why don't you simply use the already existing WordPress functions?
    Tahe a look at the get_users function for example: http://codex.wordpress.org/Function_Reference/get_users

    I went ahead and rewrote your code using the function I mentioned above, this is what I ended up with:

    <table id="rounded-corner" width="267" cellspacing="1" class="tablesorter" >
    
    	<thead>
    		<tr>
    			<th width="147" nowrap="nowrap" class="rounded-company" scope="col"><?php _e( 'Player' ); ?></th>
    			<th width="50" nowrap="nowrap" class="rounded-q2" scope="col"><?php _e( 'email' ); ?></th>
    		</tr>
    	</thead>
    
    	<tbody>
    		<?php $users = get_users(); ?>
    		<?php foreach ($users as $user) : ?>
    			<tr>
    				<td class="padded" nowrap="nowrap"><?php echo $user->user_login; ?></td>
    				<td class="padded_c" nowrap="nowrap"><?php echo $user->user_email; ?></td>
    			</tr>
    		<?php endforeach; ?>
    	</tbody>
    </table>

    The code is cleaner, simpler, it's 19 lines instead of 40 and you can be sure that no harm will come of that code. :wink:

    I hope that helps!

    Cheers,
    Ari.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.