troubleshooting Question

Setting up a PHP Class with mySQL

Avatar of mr_stevie
mr_stevieFlag for Australia asked on
PHPMySQL ServerWeb Languages and Standards
1 Comment1 Solution297 ViewsLast Modified:
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 );
		}
	}
}

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
Juan Ocasio
Continuous Process Improvement Lead

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros