PHP Classes
Icontem

File: examples.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Justin Koivisto  >  phpMysqlConnection 1.5.9  >  examples.php  
File: examples.php
Role: Example script
Content type: text/plain
Description: Some examples of how to use the class
Class: phpMysqlConnection 1.5.9
PHP class for handling MySQL database connections.
 

Contents

Class file image Download
<?php
    
// To use the class, you will first need to include the class file
    
include "class.phpMysqlConnection.php";

    
// The next step will be to set up a variable to represent the object
    // created. I usually use the name 'sql' for clarity, but it can be whatever.
    
$sql=new phpMysqlConnection($sql_user,$sql_pass,$sql_host);
    
    
// I usually use this format, and store the variables in a different
    // include file with other site-wide config settings. If you leave out
    // $sql_host from the above line, "localhost" (when MySQL server and web
    // server are on the same machine) is assumed.
    
    // Once connected, select your database that you'll be using
    
$sql->SelectDB($sql_database);
    
    
// The query format is that of mysql, so you don't need to learn any query
    // syntax with my class. All you need to do is figure out which query
    // function to use.
    
    // To get the results of a multiple row query, you would use something
    // similar to this:
    
    
$sql->Query(select idvalue from table order by id);
    for(
$i=0;$i<$sql->rows;$i++){   // for each row of results
        
$sql->GetRow($i);       // get the data for row $i

        // store row elements in variables if necessary
        
$id=$sql->data['id'];
        
$value=$sql->data['value'];
    
        
// The $sql->data array is an associative array, but if you has a query
        // similar to "select id, value, something from table", you could also get
        // the values in the for loop using something like this:
        
$id=$sql->data[0];
        
$value=$sql->data[1];

        
// do stuff with this row's data
    
}
    
    
// When you want to do a query when you know there will only be one row
    // returned, use the following function:
    
$sql->QueryRow("select id,value from table where id = '$id'");
    
    
// To get the values, just use the data array
    
$id=$sql->data['id'];
    
$value=$sql->data['value'];

    
// or
    
$id=$sql->data[0];
    
$value=$sql->data[1];
    
    
// If you want to query for a specific cell in your database, you can do so
    // like this:
    
$value=$sql->QueryItem("select value from table where id = '$id'");
    
    
// Each time you do a query, $sql->rows & $sql->data are reset, so
    // becareful when doing a loop based on query results with additional
    // queries inside the loop (unless you initialize another variable like
    // $tmp_sql).
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products