We help IT Professionals succeed at work.

Setting up a PHP Class with mySQL

293 Views
Last Modified: 2012-05-11
Hello EE,

I'm trying to set up a class using PHP that has a constructor that will create a connection to a mySQL server and I'm having trouble doing so.

If the table doesn't exist in the database, it will also do the necessary commands to create the table.

So far I have the following code:

 
class PHPClass
{
	// Variables
	var $SQLHost = "localhost";
	var $SQLDatabase = "SQL_DB";
	var $SQLUser = "username";
	var $SQLPass = "password";

        // Connection
	var $Connection;
	
	function __construct()
	{
		$Connection = new MySQLi( $SQLHost, $SQLUser, $SQLPass, $SQLDatabase );
		
		// Check if the table exists
		$SQLQuery = "SELECT * FROM table_data";
		$Query = @mysqli_query( $Connection, $SQLQuery );
		
		// If it doesn't exist, create the table and fill it with data.
		if ( !$query )
		{
			// Create the "hitcounter" table
			$SQLQuery = 	"CREATE TABLE table_data
							(
								id SMALLINT NOT NULL PRIMARY KEY,
								data SMALLINT NOT NULL
							)";
															
			// Execute Query
			@mysqli_query( $Connection, $SQLQuery );
			
			// Create the "hitcounter" table
			$SQLQuery = 	"INSERT INTO table_data VALUES (1,1);";
															
			// Execute Query
			@mysqli_query( $Connection, $SQLQuery );
		}
	}
}

Open in new window


If someone could provided me with a solution or a good source for making classes in PHP with mySQL commands, it would be greatly appreciated.
Comment
Watch Question

Continuous Process Improvement Lead
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.