Support » Everything else WordPress » query of the WP database from non-WP .php page

  • Resolved gopanthers

    (@gopanthers)


    I am trying to create A NON-WORDPRESS PAGE that accesses the wp_users table in the WordPress database and simply have it output a very simple list of all users for me. Here’s the ultra basic code I wrote:

    <?
    
    $db = mysql_connect("localhost", "DATABASE_USER", "PASSWORD");
    mysql_select_db("DATABASE_NAME",$db);
    
    $query = "SELECT * FROM wp_users ORDER BY user_login";
    $result = mysql_query ($query);
    
    if (@mysql_num_rows($result))
    {
    print "<table border=\"2\">\n"; 
    
    print "<tr>
    	<td>User Name</td>
    	<td>Email</td>
    	<td>URL</td>
    </tr>\n"; 
    
    print "<tr>\n";
        print "<td>".$row['user_login']."</td>\n"; 
    
        print "<td>".$row['user_email']."</td>\n"; 
    
        print "<td>".$row['user_url']."</td>\n"; 
    
    print "</tr>\n";
    print "</table>\n";
    
    } else { echo "<p>Sorry, no records were found!</p>"; }
    
    ?>

    I used a similar code for a different non-WordPress site and it worked fine. But I tried this (on the same domain, same server, outside of WP) and it doesn’t work. It does not give me any error message at all so it seems to be connecting to the database fine. It displays the top row which is only the header titles of User Name, Email, and URL, but then that’s it. It isn’t giving me any additional rows or any data from the database at all. It also isn’t displaying the message “Sorry, no records were found!” either.

    I’m 100% sure I have the right database name, user name, and password (which is supported by the fact that I get no error messages). It shouldn’t get much simpler than the stripped-down code I’m trying to use, so does anybody know what I’m doing wrong here?

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thanks but that’s not at all what I’m looking for. I’m not trying to integrate my blog into another site, query posts, use a theme, or have anything at all to do with the rest of WordPress at all. I’m trying to make a 100% independent webpage but I do want it to access and print out data from one particular table within the WordPress database.

    Here’s the answer: I was completely missing the while($row = mysql_fetch_array($result)) { section, so it was accessing the database but wasn’t passing that data along to be printed. Doh! The code below should work fine for anybody trying to similarly just try to simply access database data and print it in a non-WP environment.

    <?
    
    $db = mysql_connect("localhost", "DATABASE_USER", "PASSWORD");
    mysql_select_db("DATABASE_NAME",$db);
    
    $query = "SELECT * FROM wp_users ORDER BY user_login";
    $result = mysql_query ($query);
    
    if (@mysql_num_rows($result))
    {
    print "<table border=\"1\" >\n"; 
    
    print "<tr>
    	<td>User Name</td>
    	<td>Email</td>
    	<td>URL</td>
    </tr>\n"; 
    
    while($row = mysql_fetch_array($result)) { 
    
    print "<tr>\n";
        print "<td>".$row['user_login']."</td>\n"; 
    
        print "<td>".$row['user_email']."</td>\n"; 
    
        print "<td>".$row['user_url']."</td>\n"; 
    
        print "</tr>\n";
    }
    print "</table>\n"; 
    
    } else { echo "<p>Sorry, no records were found!</p>"; }
    
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘query of the WP database from non-WP .php page’ is closed to new replies.