Site icon David Yin's Blog

PHP connect DBF file

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.


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'].'&lt;br /&gt;';
echo 'Client Phone Number: '.$row['phone'].'&lt;br /&gt;';
echo '&lt;hr /&gt;';
}
odbc_close($odbc);

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

Exit mobile version