How to view foxpro registries using php

Could you help me? I need to show the registry of the .dbf table in php and have the following code.

    <form method="get">
        <input type="text" name="nrdoc"></input>
        <input type="submit"></input>

if(isset ($_GET['nrdoc'])){
$thefile = "winges/vcabdoc.DBF";
$dbf = new dbf_class($thefile);
$endexct = $timer->end();

    echo("<blockquote>File Name : $thefile<br>Number of Records : $num_rec<br>Number of Fields : $field_num</blockquote>");
    echo('<table border=1 cellspacing=0>');

    for($j=0; $j<$field_num; $j++){
        echo '<td>&nbsp;'.$dbf->dbf_names[$j]['name'];
            if ($dbf->dbf_names[$j]['type']!='M') {
                echo '<br>Length='.$dbf->dbf_names[$j]['len'];
        echo '<br>Type='.$dbf->dbf_names[$j]['type'].'</td>';
    echo '</tr>';

        if ($row === $dbf->getRow("SELECT *, FROM winges/vcabdoc.dbf WHERE 'VCANUM' == $i")) {
            echo('<td align="right">'.str_pad($i+1, 3, "0", STR_PAD_LEFT).'</td>');
                for($j=0; $j<$field_num; $j++){
                    if ($dbf->dbf_names[$j]['type']=='N') {
                        echo '<td align="right">';
                    else {
                        echo '<td align="left">';
            echo htmlentities($row[$j]).'&nbsp;</td>';
            echo '<tr>';


dbf_class.php from

And it always returns the first row of data in the database, please help me.

Thanks in advance.



source to share

1 answer

I found an easy way to extract data from a .dbf using the perl XBASE library Using this library, I wrote small scripts that read the given file and output the json string.

use File::Basename;
use XBase;

my $table = new XBase $filename or die XBase->errstr;
my @fields = $table->field_names;
my $cursor = $table->prepare_select();
my $return = '';
my $i = 0;

while (my @row = $cursor->fetch) {
  $json = '{';
  $i = 0;
  foreach $val (@row) {
    $val =~ s/(['"\/\\])/\\$1/g;
    $json .= '"'.$fields[$i].'":"'.$val.'",';
  $json  = substr($json, 0, -1);
  $json .= '},';
  $return .= $json;
$return  = substr($return, 0, -1);
print '['.$return.']';


This is how I call this file inside my php code:

exec('/usr/bin/perl /var/www/ /var/www/myfilename.dbf', $json);
$array = json_decode($json[0], true);




All Articles