Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. PHP Application Development With ADODB (part 2)
  2. Rapid Execution
  3. A Fear Of Commitment
  4. Cache Cow
  5. What's On The Menu?
  6. A Rose By Any Other Name...
  7. The Final Countdown
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 8
July 31, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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:

<?php // snip // return a CSV string echo rs2csv($result, false); ?>
And here's the revised output:

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.

 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: