#native_company# #native_desc#

Easy way to read database data using fopen()

By Dave Minor
on February 10, 2003

What if a user could select a browser bookmark and get their data?

BadBlue’s free PHP web server for Windows has a new feature called LiveSQL. LiveSQL lets users bookmark a query (or other SQL statement) in their browsers. By clicking on the link, the query is executed and data returned in several common formats. But how can PHP developers take advantage of LiveSQL?

In simple terms: the easiest way to process database data in PHP.

As you know, PHP makes it easy to read a web page using a URL. The fopen function really does all the work. LiveSQL lets you create a a simple URL that BadBlue can use as a SQL statement. For instance, a URL that selects all fields and records from the inventory table of an Access database might look like this:


Note the SQL portion of the URL (“select * from inventory”). Your PHP code can simply “fopen” the URL and immediately start reading the resulting query data in either comma-separated, tab-delimited or XML format. The format choice is yours and can be specified in the URL itself.

The following code snippet takes advantage of the fopen function and
runs a URL that performs the select:

	<html><title>LiveSQL Test</title><body><font face="verdana,arial">
		$sURL = "http://..."; // URL from LiveSQL test page
		$fd = fopen($sURL, "r");
		if ($fd) {
			while (!feof($fd)) {
				$sTemp = fgets($fd, 4096);
				$aTemp = explode("t", $sTemp);
				for ($n = 0; $n < sizeof($aTemp); $n++) {
					// Each array item now contains a field ready to
					//   saved or displayed...

This example uses a URL that generated tab-delimited data. Each line
is read using the ‘fgets’ function into a temporary string variable
named ‘stemp’. That variable is then exploded into an array so that
each field can be easily managed.

Read about LiveSQL here: http://badblue.com/helpsql.htm

Try it free here: http://badblue.com/down.htm

p.s., LiveSQL works on local -or- remote databases so you could literally fetch database data from a machine across the Internet if you wanted to!