Link to home
Start Free TrialLog in
Avatar of mr_stevie
mr_stevieFlag for Australia

asked on

Setting up a PHP Class with mySQL

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.
ASKER CERTIFIED SOLUTION
Avatar of Juan Ocasio
Juan Ocasio
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial