We help IT Professionals succeed at work.
Get Started

catching dbase_open error

Carbonecz
Carbonecz asked
on
1,646 Views
Last Modified: 2014-07-11
Hi,

I'm trying to read dbf files, but dbase_open can't read some of them because they're probably in a different format (some higher version of foxpro) so it exits with an error:

Warning: dbase_open() [function.dbase-open]: unable to open database

Open in new window

So, I found a package that can read them (PHPXBase). However it can only partially read some other formats that dbase_open reads fully.

Fatal error: cannot handle datatype

Open in new window

That's why I'm trying to catch error dbase_open throws and then try to read the file with phpxbase like this:

try {
    dbase_open ( $file_name, 0 );
}catch (Exception $e) {
    /* create a table object and open it */
    $table = new XBaseTable( $file_name );
    $table->open();

    /* print some header info */
    echo "version: ".$table->version."<br />";
    echo "foxpro: ".($table->foxpro?"yes":"no")."<br />";
    echo "modifyDate: ".date("r",$table->modifyDate)."<br />";
    echo "recordCount: ".$table->recordCount."<br />";
    echo "headerLength: ".$table->headerLength."<br />";
    echo "recordByteLength: ".$table->recordByteLength."<br />";
    echo "inTransaction: ".($table->inTransaction?"yes":"no")."<br />";
    echo "encrypted: ".($table->encrypted?"yes":"no")."<br />";
    echo "mdxFlag: ".ord($table->mdxFlag)."<br />";
    echo "languageCode: ".ord($table->languageCode)."<br />";

    /* html output */
    echo "<br /><table border=1>";

    /* print column names */
    echo "<tr>";
    foreach ($table->getColumns() as $i=>$c) {
            echo "<td>".$c->getName()." (".$c->getType()." ".$c->getLength().")</td>";
    }
    echo "</tr>";
        /* print records */
    while ($record=$table->nextRecord()) {
            echo "<tr>";
            foreach ($table->getColumns() as $i=>$c) {
                    echo "<td>".$record->getString($c)."</td>";
            }
            echo "</tr>";
    }
    echo "</table>";

    /* close the table */
    $table->close();
}

Open in new window

The problem is this doesn't work. It executes Try, shows me the "unable to open database" error and doesn't execute Catch at all.

Can you help me with this? Thanks!
Comment
Watch Question
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE