Hey.. I've been trying for quite some time now to figure out how to display statistics from my phpbb forum on my main page which is just plain html. Now I've got so far that I have an iframe which will display a PHP file called member_stat.php ... The problem is with getting the php file to read the database and output the information. I was wondering if anyone here could help me with this problem by perhaps telling me how the php code should look or what I need to do to get it to display the info I want on my main page. :-|

Dani AI

Generated

Good job to @evin getting the sample working and thanks to for pointing to the docs — a few practical improvements make the approach safer, faster, and easier to maintain today.

Prefer a single COUNT(*) query instead of pulling every user row (much lighter on the database), and exclude the guest/anonymous account by checking which row that account actually uses in the forum installation rather than assuming a specific id. Avoid running a heavy query on every page view: either cache the count for a short period or update a cached value when registrations happen. Where possible reuse phpBB’s bootstrap (include the forum’s initialization file) so credentials and session logic aren’t duplicated.

Use a modern DB API (PDO or mysqli) and a database account limited to SELECT-only for this task. Keep credentials outside the webroot or use server config constants, and never echo raw DB errors to public pages — log them instead. If the main site is static HTML, common patterns are: convert the page to .php and include the stats script, use Server Side Includes if supported, or call a small PHP endpoint via Ajax that returns JSON or a tiny HTML fragment.

A minimal PDO pattern (replace placeholders and verify the forum table prefix / anonymous id for the installation):

<?php
// member_stat.php - example (replace placeholders)
$dsn   = 'mysql:host=localhost;dbname=YOUR_DB;charset=utf8mb4';
$user  = 'readonly_user';
$pass  = 'secret';
$anonId = 1; // adjust after inspecting your phpbb_users table

$pdo = new PDO($dsn, $user, $pass, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$stmt = $pdo->prepare('SELECT COUNT(*) FROM phpbb_users WHERE user_id != ?');
$stmt->execute([$anonId]);
$total = (int) $stmt->fetchColumn();

echo '<span class="forum-stats">Members: ' . $total . '</span>';
?>

Quick troubleshooting checklist: verify the table prefix in phpBB’s config.php, confirm DB credentials and SELECT privileges, enable error logging (not display) while debugging, and test the script directly in a browser before embedding it in an iframe or via Ajax.

Recommended Answers

All 2 Replies

Here is a Daniweb thread where I show a code example to connect to a mysql db, select a db, create a query, execute it, and work with the resulting data.
http://www.daniweb.com/techtalkforums/showthread.php?t=25316&highlight=mysql_connect

There are a LOT of threads here on Daniweb in the PHP forum that will show you similar examples. You could also get almost the same code examples from the excellent PHP website.

PHP's MySQL Introduction
http://www.php.net/manual/en/ref.mysql.php

More specific code examples can be found by looking at the various mysql function documentation pages. For example, the mysql_fetch_array() documentation:
http://www.php.net/manual/en/function.mysql-fetch-array.php

Hey I ment to close this... my mistake. I figured out what was wrong with my code I will show you what I came up with.

<?php

// Defines the login information
$host = "localhost";
$database = "my_database_name";
$username = "my_login";
$password = "my_pass";

// Opens a connection and selects the database
$connect = mysql_connect("$host", "$username", "$password");
$db = mysql_select_db("$database");

// Create link identifier
$link = mysql_connect("$host", "$username", "$password");

// Performs query selcting all fileds in the table in phpbb.. phpbb_users by default
$result = mysql_query("SELECT * FROM phpbb_users", $link);

// Defined the number of rows
$num_rows = mysql_num_rows($result);

// Since there is an anonymous account subtract 1
$num_rows -= 1;

// Displays number of rows with my added font choice
echo "<font face=Verdana size=1 color=484077> $num_rows </font>";

?>

The problem I was having was with

// Performs query selcting all fileds in the table
$result = mysql_query("SELECT * FROM phpbb_users", $link);

but I got it now.. hope this serves useful to any phpbb users... :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.