phpBB2 database output

Networking/Security Forums -> Databases

Author: raztaLocation: 127.0.0.1 PostPosted: Sat Aug 12, 2006 5:02 pm    Post subject: phpBB2 database output
    ----
My PHP + MYSQL skills are minimal however ive tried to have a go at it but I cant seem to get it to work. What I want is to select how many users have registerd to my phpBB2 forums and output it to a PHP page.

Heres the code I have so far:
Code:

<?
$username="username";
$password="password";
$database="phpBB";
$host="mysql.mysite.com";


mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$sql = "SELECT count(user_id) as usercount
   FROM " . USERS_TABLE . "";

$result=mysql_query($sql); 

mysql_close();

echo $result;
?>


The page is presented with no errors however the $result is not outputed to the page either, any know where Im going wrong?

Author: GiroLocation: England PostPosted: Sat Aug 12, 2006 9:59 pm    Post subject:
    ----
Code:

<?php
[...]
$result=mysql_query($sql);
[...]
echo $result;
?>


The problem here is that you are trying to use the variable result as if it held an integer. If you look at the PHP docs you will notice that it holds a 'resource' (see this link, under heading Return values).

You need to either use mysql_fetch_row or mysql_fetch_array to obtain the actual value from the row/s.

Author: raztaLocation: 127.0.0.1 PostPosted: Mon Aug 14, 2006 12:14 pm    Post subject:
    ----
Thank you for your reply. Got it working finally with the following code.

Code:
<?
$username="username";
$password="password";
$database="phpBB";
$host="mysql1.mysite.com";

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$sql = "SELECT count(user_id) as usercount
   FROM phpbb_users";

if( !($result = mysql_query($sql)) )
echo '<b>Query: </b>' . $sql . '<br /><b>Status: </b><font color="red">Error running query (' . mysql_error() . ')</font><br /><br />';

while( $row = mysql_fetch_array($result) )
echo $row['usercount'] - 1; // So as to not count the anonymous user

mysql_close();
?>

Author: raztaLocation: 127.0.0.1 PostPosted: Mon Aug 14, 2006 1:16 pm    Post subject:
    ----
Figured the usercount code however cant figure out the query for the newest registerd username. Any one help?

Code:
<?
$username="username";
$password="password";
$database="phpBB";
$host="mysql1.mysite.com";

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$sql = "SELECT user_id, username as usercount
            FROM  phpbb_users
            WHERE user_id <> Anonymous
            ORDER BY user_id DESC
            LIMIT 1";

if( !($result = mysql_query($sql)) )
echo '<b>Query: </b>' . $sql . '<br /><b>Status: </b><font color="red">Error running query (' . mysql_error() . ')</font><br /><br />';

while( $row = mysql_fetch_array($result) )
echo $row['usercount']; // So as to not count the anonymous user

mysql_close();
?>


Got it working! Changed: "Anonymous" to: "-1"

Author: santium PostPosted: Sat Nov 04, 2006 3:54 pm    Post subject:
    ----
You can use the mysql_num_rows function in PHP to get the number of registered users.
Try this
Code:
<?php
$username = "myusername";
$password = "mypassword";
$database = "mydatabase";
$host = "localhost";

mysql_connect($host, $username, $password);
@mysql_select_db($database) or die("Problem selecting database.");

$sql_newestuser = "SELECT * FROM phpbb_users WHERE user_id > -1 ORDER BY user_id DESC LIMIT 1";
$do_newestuser = mysql_query($sql_newestuser);

$sql_usercount = "SELECT * FROM phpbb_users WHERE user_id > -1";
$do_usercount = mysql_query($sql_usercount);
while ($result = mysql_fetch_array($do_newestuser))
echo "Newest User: ".$result['username'];
echo "Total users: ".mysql_num_rows($do_usercount);
?>


As you can tell, it displays both the newest username and the total number of users. (While there is an easier way to do what this code does, this is just a rough example.



Networking/Security Forums -> Databases


output generated using printer-friendly topic mod, All times are GMT + 2 Hours

Page 1 of 1

Powered by phpBB 2.0.x © 2001 phpBB Group