Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

PHP

Posted Aug 27, 2002

Faking It - Simulated Database Queries - Page 5

By DatabaseJournal.com Staff

Cached Queries

Often we store things in databases just because it's a handy, central location. If you have a list of states and their abbreviations for a dropdown list it makes a lot of sense to put it in the database and be able to grab it when you need it. However, that data is rarely updated and is can be a performance hit to query for it. QuerySim can help take some of the load off your database by 'caching' the data in a file.


<?php 
if (!isset($states)) {
    require_once 
'DB.php';
    
$conn DB::connect(querysim);
    
$states $conn->query('
        postalAbbrev,name
        AL|Alabama
        AK|Alaska
        // and so on...
        '
);
}
?>

PHP does the work and your database gets a break.

Form Reuse

Forms may be reused by running QuerySim with NULL data before displaying the form in 'add' mode. In 'update' mode we'll run the real query.


<?php
    
if ($mode == 'add') {
        
$dsn 'querysim';
        
$sql 'userID,firstName,lastName,userGroups
                null|null|null|null'
;
    } else {
        
$dsn 'mysql://selectuser:selectpw@dbserver/appdatabase';
        
$sql "SELECT
                    userID,
                    firstName,
                    lastName,
                    userGroups
                FROM
                    userGroups
                WHERE
                    userID = '$userID'"
;
    }
    
    require_once 
'DB.php';
    
    
$conn DB::connect($dsn);
    
// error checking removed for clarity
    
$user $conn->query($sql);
    
    
$conn->disconnect();
?>

<form name="user" action="submit" method="get">
<?php 
    $userData 
$user->fetchRow(DB_FETCHMODE_ASSOC);
    
printf('<input name="userID" type="Hidden" value="%s" />'$userData['userID']);
    
printf('First Name: <input name="firstName" type="Text" value="%s" /><br />'$userData['firstName']);
    
printf('Last Name: <input name="lastName" type="Text" value="%s" /><br />'$userData['lastName']);
    
printf('User Groups: <input name="userGroups" type="Text" value="%s" /><br />'$userData['userGroups']);
    
printf('<input type="Submit" value="%s user" />'$mode);
?>
</form>

As you can see, because the display code doesn't change, QuerySim makes reusing forms a trivial matter.



PHP Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.