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 4

By DatabaseJournal.com Staff

Closing Up the 'Database'

Finally, we disconnect from the datasource:


<?php
$conn
->disconnect();
?>

disconnect() will simulate a successful disconnect. If we had connected to an external file with the DB option persistent set to true, QuerySim would close the file.

We're finished with the first fuse!

Outputting the Query

Now for dsp_UserGroups.php. Let's check the Fusedoc and dummy HTML from the prototype in the fuse stub before we start:

<fusedoc fuse="dsp_UserGroups.php" specification="2.0" language="PHP">
    <responsibilities>
        I display information about users and their groups.
    </responsibilities>
    <properties>
        <history author="Alan Richmond" type="Create" />
    </properties>
    <io>
        <in>
            <recordset name="user" primarykeys="userID">
                <number name="userID" precision="Integer" />
                <string name="firstName" />
                <string name="lastName" />
                <number name="userGroups" precision="Integer" />
            </recordset>
        </in>
    </io>
</fusedoc>
Result contains 3 rows and 4 columns

101, Stan Cox, 33
102, Hal Helms, 22
103, Bert Dawson, 11

Now we can output the results of our query. There is no difference from any other PEAR DB result set, so we don't do anything special for QuerySim:


<!-- Display users and groups -->
<?php 
printf
("Result contains <strong>%d rows</strong> and <strong>%d columns</strong>\n<br /><br />\n"$user->numRows(), $user->numCols());

while (
$row $user->fetchRow()) {
    
printf("%d, %s %s, %d\n<br />\n"$row[0], $row[1], $row[2], $row[3]);
}
?>

fetchRow() defaults to ordered output, DB_FETCHMODE_ORDERED in PEAR DB terms. If associative results are preferred and you only want the first row, change the above while to:


<?php
$row 
$user->fetchRow(DB_FETCHMODE_ASSOC0))
printf("%d, %s %s, %d\n<br>\n"$row['userID'], $row['firstName'], $row['lastName'], $row['userGroups']);
?>

Our second fuse is finished and both unit tested successfully. Our fuseaction also tested out perfectly. It output:

Result contains 3 rows and 4 columns

101, Stan Cox, 33
102, Hal Helms, 22
103, Bert Dawson, 11

Now when our DBA catches up, all we need to do is modify qry_UserGroups.php. We'll change the connect() to:


<?php
$conn 
DB:connect('mysql://selectuser:selectpw@dbserver/appdatabase');
?>

and, depending on the final database schema, we might change the query() to:


<?php
$user 
$conn->query('
    SELECT
        userID,
        firstName,
        lastName,
        userGroups
    FROM
        userGroups
'
);
?>

We don't need to touch dsp_UserGroups.php! In fact, in a large web shop, where a DBA writes the SQL for the queries, the programmer may not even be the one to add the 'real' select to the query file.

We've seen how QuerySim can play an important role in speeding application development, but its usefulness doesn't end there. Let's take a quick look at how QuerySim can be used in a production application.



PHP Archives

Comment and Contribute

 


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

 

 



















Thanks for your registration, follow us on our social networks to keep up-to-date