11th Mar, 2010 - 07:24:09 AM
IF YOU PLAN ON USING THIS WEBSITE PROPERLY, THEN PLEASE ENABLE JAVASCRIPT IN YOUR BROWSER.

YOU WILL NOT FIND ANY MALICIOUS SCRIPTING ON THIS WEBSITE.

YOUR BROWSING EXPERIENCE WILL BE SEVERLY HAMPERED IF YOU DO NOT ENABLE JAVASCRIPT BEFORE CONTINUING.

YOU WILL CONTINUE TO SEE THIS MESSAGE UNTIL YOU ENABLE JAVASCRIPT.
Your are currently not logged in. ( Login / Register )
NAME:   MySQL Class
AUTHOR:   punkstar
DATE:   29th Jan, 2006 - 06:02:37 AM
DESCRIPTION:   Streamline your MySQL querying

Need some help with that coding?


Okay, so the story goes like this.. basically, I was fed up with writing line after line to preform and utilize data that was being returned from a query, so I started browsing the internet, and found a site that had a great tutorial on a MySQL Class. I adopted this class into my coding and have used it ever since!

In this tutorial, I will show you the source of the mysql class that I picked up somewhere else, can't remember where, and then how to use it to preform the tasks that you are used to.

PHP Code
<?php
/**
* @name MySQL Class
*/
class mysql_om {

  var 
$Host     "localhost"// Hostname of our MySQL server.
  
var $Database ""// Logical database name on that server.
  
var $User     ""// User und Password for login.
  
var $Password "";

  var 
$Link_ID  0;  // Result of mysql_connect().
  
var $Query_ID 0;  // Result of most recent mysql_query().
  
var $Record   = array();  // current mysql_fetch_array()-result.
  
var $Row;           // current row number.

  
var $Errno    0;  // error state of query...
  
var $Error    "";
ews
ews
//custom
ewsvar $Query_Count 0;
ews
  
function halt($msg) {
    
printf("</td></tr></table><b>
ews                Database error:</b> %s<br>n"
$msg);
    
printf("<b>MySQL Error</b>: %s (%s)<br>n",
      
$this->Errno,
      
$this->Error);
    die(
"Session halted.");
  }
ews
  
function connect() {
    if ( 
== $this->Link_ID ) {
      
$this->Link_ID=mysql_connect($this->Host$this->User$this->Password);
      if (!
$this->Link_ID) {
        
$this->halt("Link-ID == false, connect failed");
      }
      if (!
mysql_query(sprintf("use %s",$this->Database),$this->Link_ID)) {
        
$this->halt("cannot use database ".$this->Database);
      }
    }
  }

  function 
query($Query_String) {
    
$this->connect();

#   printf("Debug: query = %s<br>n", $Query_String);

    
$this->Query_ID mysql_query($Query_String,$this->Link_ID);
    
$this->Row   0;
    
$this->Errno mysql_errno();
    
$this->Error mysql_error();
    if (!
$this->Query_ID) {
      
$this->halt("Invalid SQL: ".$Query_String);
    }
    
$this->Query_Count++;
    return 
$this->Query_ID;
  }

  function 
next_record() {
    
$this->Record mysql_fetch_array($this->Query_ID);
    
$this->Row   += 1;
    
$this->Errno mysql_errno();
    
$this->Error mysql_error();

    
$stat is_array($this->Record);
    if (!
$stat) {
      
mysql_free_result($this->Query_ID);
      
$this->Query_ID 0;
    }
    return 
$stat;
  }

  function 
seek($pos) {
    
$status mysql_data_seek($this->Query_ID$pos);
    if (
$status)
      
$this->Row $pos;
    return;
  }
ews
  
function num_rows() {
    return 
mysql_num_rows($this->Query_ID);
  }

  function 
num_fields() {
    return 
mysql_num_fields($this->Query_ID);
  }

  function 
affected_rows() {
    return @
mysql_affected_rows($this->Link_ID);
  }
}
?>


Right, this code should be saved in its own file, and then included onto every page that you want to use the class on. This is done like this:

PHP Code
<?php
include("path/to/your/file/mysql.class.php");
//then init the class
$mysql = new mysql_om;
?>


To people that are not used to OOP programming in PHP, classes are initalized, and then all of the class functions can be accessed by using the $mysql->function_name(), and then the variables accessed with $mysql->Var_Name. Its really easy to get used to.

How do I query?

Its easy, theres no mysql_connect() to do, as its all handled within the class.. so to query, i just:

PHP Code
<?php $mysql->query("SELECT * FROM `somewhere`");?>


How do I loop?

PHP Code
<?php
$mysql
->query("SELECT * FROM `somewhere`");

while(
$mysql->next_record())
{
  
$record $mysql->Record;
  
print_r($record);
}
?>

<b>Count rows?</b>

[php]<?php
$mysql
->query("SELECT * FROM `somewhere`");

echo 
$mysql->num_rows();
?>


Affected Rows?

PHP Code
<?php
$mysql
->query("SELECT * FROM `somewhere`");

echo 
$mysql->affected_rows();
?>


So, as you can see.. its very easy to use and will easily halve the amount of code you write for your mysql connections and queries. USE IT!