Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## index.php
- // Includes
- require_once("includes/class_database.php");
- // Variables
- $fooBar = '111';
- $database = new database();
- // Grab Database
- $query = "SELECT * FROM table WHERE foo_bar = '$fooBar'";
- $result = $database->database_query($query);
- $count = $database->count_rows($result);
- if($count == 0)
- {
- echo 'No results';
- echo $result;
- }
- else
- {
- // ...more code
- }
- ## includes/class_database.php
- <?
- /*
- THIS CLASS CONTAINS DATABASE RELATED FUNCTIONS
- FUNCTIONS USED IN THIS CLASS:
- init() Connects to the server and selects the database
- database_connect() Connects to the database
- database_select() Selects the database
- database_query() Queries the database
- database_error() Returns the database error
- database_close() Closes the connection to the database
- count_rows() Returns the amount of rows in a result
- */
- class database
- {
- private $db_host = 'localhost';
- private $db_user = 'root';
- private $db_pass = '';
- private $db_name = 'diwa';
- //private $database_connection = $this->database_connect($db_host,$db_user,$db_pass);
- //
- // CONNECTS TO THE SERVER AND SELECTS THE DATABASE
- //
- function init()
- {
- $this->database_connection = $this->database_connect($this->db_host, $this->db_user, $this->db_pass) or die($this->database_error());
- $this->database_select($this->db_name) or die($this->database_error());
- // This will prevent some problems on MySQL5+ servers
- mysql_query("SET sql_mode='MYSQL40'", $this->database_connection);
- }
- // END init()
- //
- // CONNECT TO THE DATABASE
- //
- function database_connect($db_host, $db_user, $db_pass)
- {
- return mysql_connect($this->host, $this->user, $this->pass, TRUE);
- }
- // END db_connect()
- //
- // THIS METHOD SELECTS A DATABASE
- //
- function database_select($database_name)
- {
- return mysql_select_db($this->db_name, $this->database_connection);
- }
- // END database_select()
- //
- // THIS METHOD QUERIES A DATABASE
- //
- function database_query($query)
- {
- $result = mysql_query($query, $this->database_connection);
- return $result;
- }
- // END db_query()
- //
- // THIS METHOD RETURNS THE DATABASE ERROR
- //
- function database_error()
- {
- return mysql_error($this->database_connection);
- }
- // END database_error()
- //
- // THIS METHOD CLOSES THE CONNECTION TO THE DATABASE SERVER
- //
- function database_close()
- {
- return mysql_close($this->database_connection);
- }
- // END database_close()
- //
- // THIS METHOD RETURNS THE NUMBER OF ROWS IN A RESULT
- //
- function count_rows($result)
- {
- if( !is_resource($result) ) return FALSE;
- return mysql_num_rows($result);
- }
- // END database_row_count()
- }
- ?>
Add Comment
Please, Sign In to add comment