Avatar of mr_stevie
mr_stevie
Flag 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.
PHPMySQL ServerWeb Languages and Standards

Avatar of undefined
Last Comment
Juan Ocasio

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Juan Ocasio

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy