Connecting Tech Pros Worldwide Forums | Help | Site Map

calling a perl program from a php script

Newbie
 
Join Date: Nov 2006
Location: washington DC
Posts: 2
#1: Nov 5 '06
Hi,
I am trying to call a perl program (that connects a webserver database) from a php program via a webbrowser. For example : http://development.noaa.gov/test_exec.php?dataid=8313. The test_exec.php script that calls the perl program is as follows:

<?php
error_log("inside test_exec \n");
$dataid = $_GET['dataid'];
$cmd = "/usr/bin/perl /........../db_connect_psql_RS3.pl ". $dataid;
error_log("dataid = $dataid\n");
exec($cmd);
?>

The code from db_connect_psql_RS3.pl is like so:
#!/usr/bin/perl

use DBI;
use strict;

# This script connects to the psql database, retrieves the data for a given dataid
# and creates a xml file with the fgdc specification.

#open the xml file to prepare the xml document
open my $FOUT, ">>client.xml";
print $FOUT "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";

#make connection to the database
my $database = "db_name";
my $hostname = "localhost";
my $username = "username";
my $password = "";

my $dsn = "DBI:Pg:dbname=$database";

#my $dbh = DBI->connect($dsn, $username, $password,{ RaiseError => 1, AutoCommit => 0 });

my $dbh = DBI->connect('DBI:Pg:dbname=cwdiscovery','brakesh','', { RaiseError => 1, AutoCommit => 0 });
...
bla
bla
bla
..
.

The db_connect_RS3.pl script is getting called. I say this because the client.xml file is getting created and the string "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" is getting prtinted to it. However the database connection is not being made, and I can't if any errors are being thrown. Can anyone please suggest any error catching mechanism when a perl script is being called by a php program. And does anyone know what is going wrong-- why is the database connectiong is not being made?. The perl script is working fine, because if I run it (stand alone) via the command line, the script produces the desired xml file for a given dataID. But the script doesn't work when called from the php program. Any pointers to solve this will be greatly appreciated.

-Bhavana.

miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#2: Nov 5 '06

re: calling a perl program from a php script


I would suggest that you add an "or die" to the creation of the database handle.

Expand|Select|Wrap|Line Numbers
  1. my $dbh = DBI->connect('DBI:Pg:dbname=cwdiscovery', 'brakesh', '', {RaiseError => 1, AutoCommit => 0})
  2.     or die "database connect failed";
  3.  
I have no experience with calling perl from a php script. So I do not know how the php script will react to a die, but it's all I can think to suggest.
Newbie
 
Join Date: Nov 2006
Posts: 2
#3: Nov 7 '06

re: calling a perl program from a php script


Quote:
#!/usr/bin/perl

use DBI;
use strict;

# This script connects to the psql database, retrieves the data for a given dataid
# and creates a xml file with the fgdc specification.

#open the xml file to prepare the xml document
open my $FOUT, ">>client.xml";
print $FOUT "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";

#make connection to the database
my $database = "db_name";
my $hostname = "localhost";
my $username = "username";
my $password = "";

my $dsn = "DBI:Pg:dbname=$database";

#my $dbh = DBI->connect($dsn, $username, $password,{ RaiseError => 1, AutoCommit => 0 });

my $dbh = DBI->connect('DBI:Pg:dbname=cwdiscovery','brakesh','', { RaiseError => 1, AutoCommit => 0 });
...
for a start you should use warnings and strict ie lines 2
use warnings;


Is there no password on the database?

have you checked /var/log/httd/error_log (assuming linux/unix here)
you can follow it by typing in a terminal tail -f /var/log/httd/error_log

If nothing is appearing in error log it may be worth converting to CGI script and checking ie:
add
use CGI
print header()
print start_html

after use strict

then print end_html at end of file
make executable and copy to cgi-bin directory
Reply