MySQL
  Home arrow MySQL arrow Page 2 - Dynamically Insert and Update Values In a MySQL Database Using OOP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
MYSQL

Dynamically Insert and Update Values In a MySQL Database Using OOP
By: Sam 'SammyK' Powers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 90
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek