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

» Database Journal Home
» Database Articles
» Database Tutorials
MS Access
SQL Scripts & Samples
» Database Forum
» Slideshows
» Sitemap
Free Newsletters:
News Via RSS Feed

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

Featured Database Articles


Posted Aug 27, 2002

Faking It - Simulated Database Queries - Page 4

By Staff

Closing Up the 'Database'

Finally, we disconnect from the datasource:


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">
        I display information about users and their groups.
        <history author="Alan Richmond" type="Create" />
            <recordset name="user" primarykeys="userID">
                <number name="userID" precision="Integer" />
                <string name="firstName" />
                <string name="lastName" />
                <number name="userGroups" precision="Integer" />
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 -->
("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:

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:


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


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.