PHP connect DBF file

| 4 Comments | No TrackBacks

DBF is very old format databse type. About 10 years ago, I use dBase which use DBF format.
Now I will show you how to connect the DBF file from PHP script.

1) Add a system DSN in ODBC Data Source Administrator
I have a source named dd.
system_dsn.gif

2) Make a file named odbc.php, the content is:
<?php
$odbc = odbc_connect ('dd', '', '') or die('Could Not Connect to ODBC Database!');
?>

3) In the PHP file, which needs to connect this database, I use the following script.

require_once('odbc.php');

$strsql= 'SELECT * FROM database.dbf';
$query = odbc_exec($odbc, $strsql) or die (odbc_errormsg());
while($row = odbc_fetch_array($query))
{
echo 'Client Name: '.$row['name'].'<br />';
echo 'Client Phone Number: '.$row['phone'].'<br />';
echo '<hr />';
}
odbc_close($odbc);


Use this way, PHP can connect the old database file, like dBase, Foxpro.


Related Articles

No TrackBacks

TrackBack URL: http://www.yinfor.com/mtcgi/mt-t-1102.cgi/2025

4 Comments

Hi David.

I am trying to connect to a .dbf file via php.
I tried your to follow your instructions.
1. Created a "dd" connection via my ODBC admin
2. Created both .php files
3. Created a small dbf file with three name/phone records.
When I run the main php script, I receive the following error message:
Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][Pilote ODBC dBase] Syntax error in clause FROM., SQL state 37000 in SQLExecDirect in c:\Web\essai\testdbase.php on line 5
[Microsoft][Pilote ODBC dBase] Syntax error in FROM clause.

The files are currently on my test server. You can test them at:
http://www.pconno.gotdns.com/essai/testdbase.php

Seems that:
1. The connection works fine (I receive no "die" )message
2. The dbase file is found ok
3. The querry string is somehow defective.

Would you have any suggestion?

Thanks a lot,
Pierre

I have get back to you by email. I think it should be OK now.

Dear David,

where are your databases (dbf)? I don't know that where should i put it?

Pls advise me.

Many thanks,
Jack

The dbf files are saved in any folder. Say, I have three dbf files in c:\data-dbf\.
When I add system DSN and select Microsoft dBase drive, it will let me choose the folder.

Any dbf file in this folder can be accessed by this way.

Leave a comment

About this Entry

This page contains a single entry by David Yin published on January 11, 2008 10:42 PM.

Conver text to date in PHP was the previous entry in this blog.

Solid ink printing from Xerox is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.21-en

Online Tools