open = mysql_connect($this->hostname, $this->username, $this->password); if (!$this->open) { echo "MySQL-Error #: " . mysql_errno() . "\n
\nMySQL-Error: " . mysql_error() . "\n
\n
\n"; } $this->select = mysql_select_db($this->database); if (!$this->select) { echo "MySQL-Error #: " . mysql_errno() . "\n
\nMySQL-Error: " . mysql_error() . "\n
\n
\n"; } return $this->select; } # Query the database for requested information # Timer for calculation of query-speed is started and number of queries is counted # If any error occurs return error number and error message otherwise the result function query($mysqldbQuery) { $sql_starttime = microtime(); $this->query = mysql_query($mysqldbQuery); $sql_endtime = microtime(); $GLOBALS['class']['sql_queries']++; $GLOBALS['class']['sql_gentime'] = round(($sql_endtime - $sql_starttime), 3) + $GLOBALS['class']['sql_gentime']; if ($this->query == false) { echo "MySQL-Error #: " . mysql_errno() . "\n
\nMySQL-Error: " . mysql_error() . "\n
\nMySQL-Query: " . $mysqldbQuery . "\n
\n
\n"; } return $this->query; } # If returned query needs to be processed in an array it will be fetched and result will be returned # Check if an argument is given or if the function is called seperately function fetch($fetchedQuery) { $fetchedQuery == 0 ? $this->fetch = mysql_fetch_array($this->query) : $this->fetch = mysql_fetch_array($fetchedQuery); return $this->fetch; } # If affected rows from a query are needed this function will handle it # Check if an argument is given or if the function is called seperately function num_rows($numrowsQuery) { $numrowsQuery == 0 ? $this->num_rows = mysql_num_rows($this->query) : $this->num_rows = mysql_num_rows($numrowsQuery); return $this->num_rows; } # Disconnect from the database # If any error occurs return error number and error message otherwise the result function shut() { $this->shut = mysql_close($this->open); if (!$this->shut) { echo "MySQL-Error #: " . mysql_errno() . "\n
\nMySQL-Error: " . mysql_error() . "\n
\n
\n"; } return $this->shut; } } # Initiate the class $mysql = new mysql(); # This class offers several functions which are used on almost every section # MySQL-related stuff is also needed in this class so it must be transmitted class tools extends mysql { # For statistic-purposes return number of page-requests in the last 24 hours # (with some help from Ian Brooks at http://www.phpBBHacks.com) function usage() { # Connect to database and delete all entries which are outdated $this->open(); $this->query("DELETE FROM server_usage WHERE time < " . (time() - 86400)); # Insert a new entry in the database and query the new number of entries # Disconnect from the database and return the number $this->query("INSERT INTO server_usage (time, ip) VALUES (" . time() . ", '" . $_SERVER['REMOTE_ADDR'] . "')"); $this->query("SELECT time FROM server_usage"); $this->num_rows(); $this->shut(); return $this->num_rows; } # For statistic-purposes this returns the MySQL-version function version() { # Query the database for MySQL-version and fetch the result # Returned string contains "-log" at the end so cut it off $this->open(); $build = $this->fetch($this->query("SELECT VERSION()")); $this->shut(); $this->version = substr($build[0], 0, -4); return $this->version; } } # Initiate the class $tool = new tools(); $mysql->query("SELECT id FROM news"); $mysql->query("SELECT cid FROM comments"); echo '
# Queries: ' . $class['sql_queries']; echo '
Query-Time: ' . $class['sql_gentime'] . '
'; $tool->version(); $tool->usage(); echo '
# Queries: ' . $class['sql_queries']; echo '
Query-Time: ' . $class['sql_gentime']; ?>