HomeMySQL Page 2 - Dynamically Insert and Update Values In a MySQL Database Using OOP
Essential Connection - MySQL
Stop writing insert and update SQL statements and cut the time you spend writing simple SQL in half while focusing on the more complicated things. Leave it up to OOP to help you out. We will make a class that goes out and looks for the values for us and builds a SQL statement on the fly. All we have to do is make sure the column names in the database correspond with the field names in the HTML form. Believe me when I say it saves TONS of time. I never write applications that don't use it.
This article assumes you have basic knowledge of what objects are. If you don't know what objects are, you might want to check out icarus's article entitled Back To Class. Don't worry if you are not an expert at OOP yet since the OOP used in this article is quite basic.
So let's create our class and add two methods to it, a method to connect to a database, and one to disconnect from a database.
class MyDatabase
{ // The var that stores the last // used SQL statement var $SQLStatement = "";
// The var that stores the error // (if any) var $Error = "";
function MyDatabase() { // Config for the database // connection $this->DBUser = "tmp_usr"; $this->DBPass = "<A href="mailto:super#secret@pass">super#secret@pass</A>"; $this->DBName = "c_test"; $this->DBHost = "localhost"; }
function Connect() { //Connect to a mysql database $this->db = mysql_connect($this->DBHost, $this->DBUser, $this->DBPass) or die("MYSQL ERROR: ".mysql_error()); // Select the database mysql_select_db($this->DBName, $this->db) or die("MYSQL ERROR: ".mysql_error()); }
// Disconnect from the MYSQL database function Disconnect() { mysql_close($this->db) or die("MYSQL ERROR: ".mysql_error()); } }
The class is named MyDatabase simply because this is what I named it about two years ago. It's a corny name that stuck. I can think of many variables, files, and "other" that got irrationally named something ridiculous but still remain the same name today. Anyway, what I was trying to get at was you can name it whatever fits your fancy.
var $SQLStatement
= ""; var $Error = "";
$SQLStatement - Stores the SQL statement that will be dynamically created later.
$Error - Will store an error message should any problems arise.
The first method is relatively simple. It is named MyDatabse (the class name) so that it is executed when the class is instantiated, and all it does is set the username, password, database name, and host for the MYSQL database.
function Connect
() { // Connect to a mysql database $this->db = mysql_connect($this->DBHost, $this->DBUser, $this->DBPass) or die("MYSQL ERROR: ".mysql_error()); // Select the database mysql_select_db ($this->db, $this->{$this->DBConnection}) or die("MYSQL ERROR: ".mysql_error()); }
This method takes the properties set by MyDatabase and uses them to connect to the MySQL server, and then select the database.
function Disconnect
() { mysql_close($this->db) or die("MYSQL ERROR: ".mysql_error()); }
This method simply closes the MySQL connection.
We will use these in just a minute, but for now let's create the AddToDB method so we can start populating our database.