PHP Classes

File: examples.php

Recommend this page to a friend!
  Classes of Justin Koivisto   phpMysqlConnection 1.5.9   examples.php   Download  
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.
Author: By
Last change: Updated the examples page.
Date: 21 years ago
Size: 2,509 bytes
 

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 id, value 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).
?>