HomePHP Page 6 - PHP Application Development With ADODB (part 2)
A Rose By Any Other Name... - PHP
In this concluding article, find out about ADODB's advancedfunctions, with examples that demonstrate how ADODB can be used tooptimize multiple-run queries, commit and roll back transactions,improve performance by caching query results, and automatically writeHTML (ortext) files.
ADODB also allows you to export a resultset into a variety of different formats - comma-separated text, tab-separated text, or even an HTML table. These functions are not part of the ADODB class per se; rather, they are packaged as ancillary functions in a separate file, which needs to include()-d in your scripts. The following example demonstrates:
<?php
// uncomment this to see plaintext output in your browser
// header("Content-Type: text/plain");
// include the ADODB library
include("adodb.inc.php");
// include conversion functions
include("toexport.inc.php");
// create an object instance
// configure it for a MySQL connection
$db = NewADOConnection("mysql");
// open connection to database
$db->Connect("localhost", "john", "doe", "db278") or die("Unable to
connect!");
// execute query
$query = "SELECT title, id FROM library";
$result = $db->Execute($query) or die("Error in query: $query. " .
$db->ErrorMsg());
// return a CSV string
echo rs2csv($result);
// close database connection
$db->Close();
?>
Here's the output:
title,id
Mystic River,15
Where Eagles Dare,16
XML and PHP,17
You can suppress the first line - the column list - by adding
an extra argument to the call to rs2csv(), like this:
Mystic River,15
Where Eagles Dare,16
XML and PHP,17
You can format the data as a tab-separated string with the
rs2tab() function,
<?php
// uncomment this to see plaintext output in your browser
// header("Content-Type: text/plain");
// include the ADODB library
include("adodb.inc.php");
// include conversion functions
include("toexport.inc.php");
// create an object instance
// configure it for a MySQL connection
$db = NewADOConnection("mysql");
// open connection to database
$db->Connect("localhost", "john", "doe", "db278") or die("Unable to
connect!");
// execute query
$query = "SELECT title, id FROM library";
$result = $db->Execute($query) or die("Error in query: $query. " .
$db->ErrorMsg());
// return a tab-separated string
echo rs2tab($result);
// close database connection
$db->Close();
?>
which returns the following output:
title id
Mystic River 15
Where Eagles Dare 16
XML and PHP 17
or as an HTML table with the rs2html() function,
<html>
<head></head>
<body>
<?php
// uncomment this to see plaintext output in your browser
// header("Content-Type: text/plain");
// include the ADODB library
include("adodb.inc.php");
// include conversion functions
include("tohtml.inc.php");
// create an object instance
// configure it for a MySQL connection
$db = NewADOConnection("mysql");
// open connection to database
$db->Connect("localhost", "john", "doe", "db278") or die("Unable to
connect!");
// execute query
$query = "SELECT title, id FROM library";
$result = $db->Execute($query) or die("Error in query: $query. " .
$db->ErrorMsg());
// return a table
echo rs2html($result);
// close database connection
$db->Close();
?>
</body>
</html>
which looks like this:
A
number of other interesting conversion functions are also shipped with ADODB - take a look at the documentation for more information.