Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * database.class.php
- */
- class database {
- static private $obj = NULL; // storage for a database class instance
- private $hostname = 'localhost';
- private $username = 'root';
- private $password = 'root';
- private $database = 'test';
- private $db_connection; // storage for a mysql resource identifier
- function __construct() {
- // setup the database connection on construct
- $this->db_connection = mysql_connect($this->hostname, $this->username, $this->password, true);
- mysql_select_db($this->database, $this->db_connection);
- }
- static function getInstance() {
- // return our instance of the database class, create it if it does not already exist
- if (self::$obj == NULL) {
- self::$obj = new database;
- }
- return self::$obj;
- }
- public function query($query) {
- return mysql_query($query, $this->db_connection);
- }
- }
- ?>
- <?php
- /*
- * news.class.php
- */
- class news {
- function post($title, $content) {
- return '<a href="#"><h2>'.$title.'</h2></a>'.
- '<p>'.$content.'</p>';
- }
- function show($amount) {
- $query = database::getInstance()->query('SELECT id, date, title, content FROM archive ORDER BY date DESC LIMIT '.$amount);
- while ($fetch = mysql_fetch_array($query, MYSQL_ASSOC)) {
- return '<a id="'.$fetch['id'].'"><h2>'.$fetch['title'].'</h2></a>'.
- '<p>'.$fetch['content'].'</p>';
- }
- return '<p>show '.$amount.'</p>';
- }
- }
- ?>
Add Comment
Please, Sign In to add comment