phptutorials.cjb.net - Free PHP Tutorials and Advice
 

PHP Classes Tutorial 1 - Database Abstraction and Classes Tutorial - Page 6 of 7

Code Listing

This is the full listing for phpDBConnector.php


<?php
	class phpDBConnector
	{
		var $connect_id; // needed to keep the database connection open

		function phpDBConnector( $host, $username, $password )
		{
			$this->connect_id = @mysql_connect( $host, $username, $password );
		}

		function selectDB( $db_name )
		{
			mysql_select_db( $db_name, $this->connect_id );
		}

		function query( $query )
		{
			return mysql_query( $query, $this->connect_id );
		}

		function fetch_Object( $query )
		{
			return mysql_fetch_object( $query );
		}

		function fetch_Array( $query )
		{
			return mysql_fetch_array( $query );
		}

		function fetch_Row( $query )
		{
			return mysql_fetch_row( $query );
		}

		function getInsertID()
		{
			return mysql_insert_id();
		}

		function num_rows( $result )
		{
			return mysql_num_rows( $result );
		}

		function getDBList()
		{
			return  mysql_list_dbs();
		}

		function getTableList( $database )
		{
			return mysql_list_tables( $database );
		}

		function getFieldList( $database, $table )
		{
			return mysql_list_fields( $database, $table );
		}

		function createDB( $db_name )
		{
			$sql = 'CREATE DATABASE ' . $db_name;
			mysql_query( $sql, $this->connect_id );
		}

		function dropDB( $db_name )
		{
			$sql = 'DROP DATABASE ' .$db_name;
			mysql_query( $sql, $this->connect_id );
		}

		function table_exists( $table )
		{
			if( mysql_query( 'SELECT 1 FROM \'' . $table . '\' LIMIT 0', $this->connect_id ) ) 
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
?>