Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Win32/DBD::ODBC: works from cmd.exe, not works from apache (xampp)

by alexander_lunev (Pilgrim)
on Nov 28, 2016 at 06:14 UTC ( [id://1176681]=perlquestion: print w/replies, xml ) Need Help??

alexander_lunev has asked for the wisdom of the Perl Monks concerning the following question:

Hello, monks!

I'm not sure it it's a Perl issue, but it is somehow related to it, so i'll give it a try.

I have a Windows 2003 server, and an old database in DBF files, and a need to work with this DB. Numerous scripts are works fine when started from command line, doesn't matter through Win32::ODBC or DBI::ODBC, but when they started from local apache web server (XAMPP, running under the same user), the shows me an error:

ERROR ODBC: [-1022] [] "[Microsoft][ODBC dBase Driver]  Disk or network error."

or, if i use DBD::ODBC:

ERROR ODBC: [Microsoft][ODBC dBase Driver] Disk or network error. (SQL-HY000)

But when i run the same script from command line, everything works fine:

w:\xampp\htdocs>errors.pl 2009-02-27 2005-01-01 2010-01-20 2008-10-01 2012-09-28 2016-08-30 2010-06-18 2010-05-18 2010-03-01 2009-12-23 2010-02-14

the code itself:

my $DSN = "EXDSN"; my $Dir = "e:/db/"; my $Driver = "Microsoft dBase Driver (*.dbf)"; if (!Win32::ODBC::ConfigDSN(ODBC_ADD_SYS_DSN, $Driver, "DSN=$DSN", "Description=DSN", "DBQ=$Dir", "DEFAULTDIR=$Dir", "UID=" )) { die "ERROR ODBC: Can't config Win32::ODBC : ".Win32::ODBC::Error() +; } if ($odbctype eq "DBI") { my $EXDSN = "driver=$Driver;dbq=$Dir;defaultdir=$Dir"; $db = DBI->connect("dbi:ODBC:$EXDSN","","" ); die "ERROR ODBC: ". wenc(scalar $DBI::errstr) ."\n" if not $db; } if ($odbctype eq "WIN32") { $db = new Win32::ODBC( $DSN ); die "ERROR ODBC: ". wenc(scalar Win32::ODBC::Error()) ."\n" if not + $db; } if ($odbctype eq "WIN32") { if ( $db->Sql( "select table.date from table group by table.date o +rder by table.date desc" )) { my $err = $db->Error(); print "Error DBF SQL: " . &enc($err) . "\n"; } else { while($db->FetchRow() ) { my(%Data) = $db->DataHash(); print Dumper(%Data); } } } if ($odbctype eq "DBI") { my $ref_astore = $db->selectall_hashref( "select table.date from t +able group by table.date order by table.date desc",'date' ); if (!defined($ref_astore)) { print "Error DBF: " . $db->errstr . "\n"; exit; } else { print join("\n",keys %{ $ref_astore } ); } }

Although i can have access to *.dbf files and DSN list of ODBC driver from apache. The code:

my $dir = "e:/db/"; if (-d $dir) { print "OK $dir\n"; opendir(my $dh, $dir) || die "Can't opendir $dir: $!"; print join("\n",readdir($dh)); closedir $dh; } my %dsn = Win32::ODBC::DataSources(); foreach my $key (sort keys %dsn) { print "<br>$key $dsn{$key}"; }

gives me the output:

OK e:/db/ . .. TABLE.DBF TABLE2.DBF TABLE3.DBF ... DB Microsoft dBase Driver (*.dbf) DSN Microsoft dBase Driver (*.dbf) EXDSN Microsoft dBase Driver (*.dbf)

What's the problem here?

UPDATE:

SOLVED thanks to Corion. Seems that for ODBC to work it needs TMP environment variable. Setting it in code before ODBC stuff did the trick.

 $ENV{'TMP'} = 'C:\\DOCUME~1\\lan\\LOCALS~1\\Temp\\2';

Replies are listed 'Best First'.
Re: Win32/DBD::ODBC: works from cmd.exe, not works from apache (xampp)
by afoken (Chancellor) on Nov 28, 2016 at 06:27 UTC
    ERROR ODBC: [-1022] [] "[Microsoft][ODBC dBase Driver] Disk or networ +k error." ERROR ODBC: [Microsoft][ODBC dBase Driver] Disk or network error. (SQL +-HY000)

    Did you search for the error codes? SQL-HY000 seems to be quite common, according to a 10 sec Google search, and there is at least one related hotfix available at microsoft.com.

    my $dir = "e:/db/";

    Is E: a network drive? Drive letters on modern Windows versions are (usually) per session, so E: might not be mapped or mapped with wrong permisssions in the service.

    when they started from local apache web server (XAMPP, running under the same user)

    As far as I remember, Windows services have some restrictions. The most trivial one is that services are usually not allowed to interact with the desktop, i.e. you can't open a window (including message boxes) from a service.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      E: is not a network drive, and if it was then script would not work from cmd.exe either. But it works from cmd.exe.

      Apache/XAMPP is not started as a service, but as a regular process under the same user (me) that is starting the same script from cmd.exe.

      I would understand if a Perl from apache couldn't have access to files in e:/db/. But it can list the files.

      I would understand if a Perl from apache couldn't have access to ODBC. But it can list system DSNs from ODBC driver.

        What happens if you do system $^X, 'full/path/to/file/that/works/from/cmd.pl' from within apache program?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1176681]
Approved by Athanasius
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-03-28 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found