PHP Classes

File: DatabaseQueriesExample.php

Recommend this page to a friend!
  Classes of Sujay Bhowmick  >  Database Queries  >  DatabaseQueriesExample.php  >  Download  
File: DatabaseQueriesExample.php
Role: Example script
Content type: text/plain
Description: Example Script
Class: Database Queries
Abstraction for executing database queries
Author: By
Last change:
Date: 18 years ago
Size: 2,309 bytes
 

Contents

Class file image Download
<?php
include_once("DatabaseQueriesFactory.php");
include_once(
"ConnectionManagerFactory.php");
include_once(
"DBTypes.php");


 
/**
  * Get the corresponding ConnectionManager object w.r.t to database specified in DB_TYPE constant(DBTypes.php).
  * Factory Method. One can use their own connection object. I used here ConnectionManager Class to connect to the database.
  * You can find the ConnectionManager Class at the following url http://www.phpclasses.org/browse.html/package/1429.html
  */
$objConnectionClass = ConnectionManagerFactory::getInstanceOf(DB_TYPE);

/**
 * Invoke the doConnection object to make a connection to the specified database
 */
$objConnectionClass->doConnection();

/**
 * Get the connectionHandle (Base Class Method).
 */
$conn = $objConnectionClass->getConnectionHandle();

/**
 * Select the database.
 */
$objConnectionClass->selectDatabase();

$selquery = "select * from alumini order by firstname";



/**
 * $edited = "F";
 * $selquery = "SELARTICLES"; //(Procedure name).
 * $params = array("@edited"=>array($edited=>SQLCHAR, false)); // (Procedure Parameters).
 */
   
$params = null;

/**
 * Get the corresponding DatabaseQueries object w.r.t to database specified in DB_TYPE constant(DBTypes.php).
 * Factory Method.
 */

/**
 * if $params in null it means the query is of type inline otherwise it is a procedure and provide the $params associative array
 * as follows:-
 * $params -- Associative array eg. array("@edited"=>array($edited=>SQLCHAR, false));
 * where -- @edited is input/output paramter,
 * -- $edited is the value of Input Parameter @edited,
 * -- SQLCHAR is a the MSSQL Constant for CHAR column type,
 * -- false indicates @edited is not an output parameter.
 */
       
$objDatabaseClass = DatabaseQueriesFactory::getInstanceOf($selquery, $conn, $params, DB_TYPE);

/**
 * Call the executeQuery method.
 */
if(!$objDatabaseClass->executeQuery())
{
    die(
"Cannot query");
}

 
/* $result = your resultset fetched from database by calling base class method getResultSet(). */

$result = $objDatabaseClass->getResultSet();
$row = $objDatabaseClass->getResultArray();
echo
$row["firstname"];
 
/**
 * get the number of rows in a result set.
 */
$rowcount = $objDatabaseClass->getNumRows();


unset(
$objConnectionClass);
unset(
$objDatabaseClass);
?>