24.05.2016, 08:31
(
Последний раз редактировалось JasperM; 16.08.2016 в 13:10.
)
A few things to note:
1. MySQL is deprecated in PHP 5 and completely abandoned in PHP 7 (in which it throws Fatal Errors at you). As specified above use MySQLi if you want something simple and easy to learn if you already know MySQL_*, or use PDO if you want something a little bit more advanced with support for Object-Oriented Programming.
2. Use classes for easier access accross pages and make one config.php file which you require_once on every front-end page. In this config, configure an autoloader which automatically loads the required classes when needed. In this config you can also store database credentials and such, making all other files a lot cleaner.
3. I would do something like this:
config.php
User.class.php
banlist.php
With such a folder structure:
Root
|-- assets
| |-- classes
| | `-- User.class.php
| `-- config.php
`-- banlist.php
1. MySQL is deprecated in PHP 5 and completely abandoned in PHP 7 (in which it throws Fatal Errors at you). As specified above use MySQLi if you want something simple and easy to learn if you already know MySQL_*, or use PDO if you want something a little bit more advanced with support for Object-Oriented Programming.
2. Use classes for easier access accross pages and make one config.php file which you require_once on every front-end page. In this config, configure an autoloader which automatically loads the required classes when needed. In this config you can also store database credentials and such, making all other files a lot cleaner.
3. I would do something like this:
config.php
PHP код:
<?php
// Database credentials
define("DB_HOST", "localhost");
define("DB_USER", "username");
define("DB_PASS", "password");
define("DB_DATABASE", "database_name");
// Connecting to database
$dsn = "mysql:dbname=" . DB_DATABASE . ";host=" . DB_HOST;
try {
$db_con = new PDO($dsn, DB_USER, DB_PASS);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
// Configuring autoloader
// Example: if a new instance of User is created, it automatically requires /assets/classes/User.class.php
// If your classes are in a different folder, change the include line
spl_autoload_register(function($class) {
include "assets/classes/" . $class . ".class.php";
});
$user = new User($db_con);
?>
PHP код:
<?php
class User {
private $db;
/**
* Creating a new user instance
* @param PDO $db_con PDO database connection
*/
function __construct($db_con) {
$this->db = $db_con;
}
/**
* Gets the amount of bans
* @return int Amount of bans
*/
public function getBannedCount() {
$stmt = $this->db->prepare("SELECT COUNT(*) FROM bans");
$stmt->execute();
return (int) $stmt->fetchColumn();
}
/**
* Gets all the bans
* @return array Array with all bans, contains TimeData, PlayerName, By and Reason
*/
public function getBannedList() {
$stmt = $this->db->prepare("SELECT TimeDate, PlayerName, By, Reason FROM bans");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
?>
PHP код:
<?php
require_once("assets/config.php");
$amount = $user->getBannedCount();
$bans = $user->getBannedList();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ban list</title>
</head>
<body>
<p>There are currently <?= $amount; ?> players banned.</p>
<table>
<thead>
<tr>
<th>Time and date of ban</th>
<th>Playername</th>
<th>Admin name</th>
<th>Reason</th>
</tr>
</thead>
<tbody>
<?php
foreach($bans as $ban) {
echo "<tr>";
echo "<td>" . $ban["TimeDate"] . "</td>";
echo "<td>" . $ban["PlayerName"] . "</td>";
echo "<td>" . $ban["By"] . "</td>";
echo "<td>" . $ban["Reason"] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</body>
</html>
Root
|-- assets
| |-- classes
| | `-- User.class.php
| `-- config.php
`-- banlist.php