MySQL
  Home arrow MySQL arrow Page 2 - Dynamically Insert and Update Values I...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Dynamically Insert and Update Values In a MySQL Database Using OOP
By: Sam 'SammyK' Powers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 83
    2004-02-04

    Table of Contents:
  • Dynamically Insert and Update Values In a MySQL Database Using OOP
  • Essential Connection
  • Adam Up
  • If You POST It, It Will Go
  • Updateagenessly
  • UpdateDB in Action
  • You Wouldn't Have to Update It Had You Gotten It Right the First Time

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Dynamically Insert and Update Values In a MySQL Database Using OOP - Essential Connection


    (Page 2 of 7 )

    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


     

       

    MYSQL ARTICLES

    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway