Quote:
Originally Posted by ǝnd1ǝss-ɯonǝʎ
Proper version:
Put this in your backend:
PHP Code:
function usercount() { $usercount = mysql_query("SELECT COUNT(*) FROM users") or die(mysql_error());
echo "Registered:".mysql_result($usercount,0); }
and now you can use this function on various places of your website.
+ much faster
+ execution time does not depend on how big the db is
+ reusable
|
It is a lot faster to use functions.
And if you even build-ed it into one of the existing class files, then you would save space.
And reusable when you have required it in an init file, so you would just need to use:
PHP Code:
$userb->playerCount();
Or if it is in same class script:
PHP Code:
self::playerCount();
Here is my examples of what you could do.
And it is also reusable, and uses pdo.
Example from my server-config
PHP Code:
## SQL Server Data
/*
$Config['MySQL'] = [
'host' => 'localhost',
'user' => 'root',
'pass' => '',
'dbname' => 'darkorbit'
];
*/
$Config = new stdClass();
$Config->MySQL = new stdClass();
$Config->MySQL->host = "localhost";
$Config->MySQL->user = "root";
$Config->MySQL->pass = "";
$Config->MySQL->dbname = "darkorbit";
define( "DB_DSN", "mysql:host=".$Config->MySQL->host.";dbname=".$Config->MySQL->dbname.""); //this constant will be use as our connectionstring/dsn
define( "DB_USERNAME", $Config->MySQL->user ); //username of the database
define( "DB_PASSWORD", $Config->MySQL->pass ); //password of the database
Example class code returning the total number of the table: Class.Userbase.php
PHP Code:
class Userbase{
private $con;
public function __construct() {
$this->con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function playerCount(){
$query = $this->con->prepare("SELECT COUNT(*) FROM tablename");
if ($query->execute()) {
$count = $query->rowCount();
return $count
}
}
}
This should actually work with azure->boxxy/madatek version.
And remember to edit the init.php
PHP Code:
require_once 'Class.Userbase.php';
$userb= new Userbase();
Then this on the front page should actually be enough.
PHP Code:
print_r($userb->playerCount());
I love PDO *-*
Best Regards RQ