Home arrow MySQL arrow 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.

TABLE OF CONTENTS:
  1. Dynamically Insert and Update Values In a MySQL Database Using OOP
  2. Essential Connection
  3. Adam Up
  4. If You POST It, It Will Go
  5. Updateagenessly
  6. UpdateDB in Action
  7. You Wouldn't Have to Update It Had You Gotten It Right the First Time
By: Sam 'SammyK' Powers
Rating: starstarstarstarstar / 96
February 04, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.


function MyDatabase()
{
 $this
->DBUser "tmp_usr";
 $this
->DBPass "super#secret@pass";
 $this
->DBName "c_test";
 $this
->DBHost "localhost";
}

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.



 
 
>>> More MySQL Articles          >>> More By Sam 'SammyK' Powers
 

blog comments powered by Disqus
   

MYSQL ARTICLES

- Xeround Releases Free Version of MySQL Cloud...
- Oracle Announces New MySQL Specialization
- Constant Contact Chooses SkySQL for MySQL Su...
- Revoke Statement in MySQL
- The Grant Statement in MySQL
- SuccessBricks Announces ClearDB Availability...
- Building a PHP ORM: Deploying a Blog
- TROSYS Launches Free MySQL Manager and Admin...
- Building an ORM in PHP: Domain Modeling
- Building an ORM in PHP
- MySQL Leads Open Source Market, Gets Cluster...
- Oracle Announces Milestone Release for MySQL
- How to Stop SQL Injection Attacks
- New Defragmentation Solution for SQL Server
- Comparison of MyISAM and InnoDB MySQL Databa...


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: