how to connect database from web
Posted in 1999
Topics: Error Codes & Troubleshooting, Connectivity: ESQL/C, 4GL & Embedded SQL, Security, Permissions & Auditing, Platform-Specific Issues
Hi,
I use Linux with Informix SE, ESQL/C, ISQL 7.20.
I want to run my EC program on the web browser to insert/select data from
my informix server. I can run my EC program correctly and insert the data to
the database. But if I run it on the web then it cannot open the database,
the sqlca.sqlcode=-461
Originally, i think maybe i didn't give the program the userid and pwd,
but after i tried it's the same.....
Here is my EC program "add.ec".....
-------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
$include sqlca;
void main(int argc,char *argv[]){
char name[20],addr[40],tel[10],item_list[1024+1];
$char sql_insert[1024+1],pwd[15+1];
strcpy(pwd,"mypassword");
$ connect to '/home/frm/tt@dbs_rm' user 'peary' using $pwd;
/* $ database 'tt@dbs_rm'; */
if(sqlca.sqlcode<0){
printf("database open error!\\n");
exit(0);
}
strcpy(name,argv[1]);
strcpy(addr,argv[2]);
strcpy(tel,argv[3]);
sprintf(item_list,"('%s','%s','%s')",name,addr,tel);
sprintf(sql_insert,"%s %s\\n","insert into base values",item_list);
$ prepare cur from $sql_insert;
$ execute cur;
$ free cur;
$ disconnect current;
exit(1);
}
------------------------------
I use "esql -o add add.ec" compiled it to "add", and if i run "./add 123 456
789"
under Linux, then it can connect to the database and insert the 3 datas.
But if I run it on the web( use perl to call ec program), then it always
returns
"database open error!".
Here is my perl program "tt.pl"...
------------------------------
#!/usr/bin/perl
use CGI qw/:standard :html3/;
use CGI::Carp qw(fatalsToBrowser);
use CGI::Carp qw(carpout);
use strict;
require "pl_lib.pl";
$|=1;
print "Content-type:text/html\\n\\n";
my %FORM=read_input(); #-- to analyse the form data
print <<eof;
<p><h1>Thank you for your coming!!!</h1>
name:$FORM{'name'}<p>
addr:$FORM{'addr'}<p>
tel:$FORM{'tel'}<p>
eof
print "C starts....<br>";
my $val=qx(./add $FORM{'name'} $FORM{'addr'} $FORM{'tel'})||die $!;
print "return value--$val<br>";
print "C ends...<br>";
--------------------------------
The perl script can run correctly , so the problem is database....
Could you help me again? Thank you~~~
Peary
I think ./add is runned by user nobody. (apache and others default)
Check ACLs.
Jeepee
peary a 'crit dans le message <7idn32$rik$1@news1.sinica.edu.tw>...
>Hi,
>
>I use Linux with Informix SE, ESQL/C, ISQL 7.20.
>I want to run my EC program on the web browser to insert/select data from
>my informix server. I can run my EC program correctly and insert the data
to
>the database. But if I run it on the web then it cannot open the database,
>the sqlca.sqlcode=-461
>Originally, i think maybe i didn't give the program the userid and pwd,
>but after i tried it's the same.....
>Here is my EC program "add.ec".....
>-------------------------------------
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>$include sqlca;
>
>void main(int argc,char *argv[]){
> char name[20],addr[40],tel[10],item_list[1024+1];
> $char sql_insert[1024+1],pwd[15+1];
>
> strcpy(pwd,"mypassword");
>
>$ connect to '/home/frm/tt@dbs_rm' user 'peary' using $pwd;>
>/* $ database 'tt@dbs_rm'; */
>
> if(sqlca.sqlcode<0){
> printf("database open error!\\n");
> exit(0);
> }
>
> strcpy(name,argv[1]);
> strcpy(addr,argv[2]);
> strcpy(tel,argv[3]);
>
> sprintf(item_list,"('%s','%s','%s')",name,addr,tel);
> sprintf(sql_insert,"%s %s\\n","insert into base values",item_list);
>
>$ prepare cur from $sql_insert;
>$ execute cur;
>$ free cur;>
>$ disconnect current;>
> exit(1);
>}
>
>------------------------------
>I use "esql -o add add.ec" compiled it to "add", and if i run "./add 123
456
>789"
>under Linux, then it can connect to the database and insert the 3 datas.
>But if I run it on the web( use perl to call ec program), then it always
>returns
>"database open error!".
>Here is my perl program "tt.pl"...
>------------------------------
>
>#!/usr/bin/perl
>use CGI qw/:standard :html3/;
>use CGI::Carp qw(fatalsToBrowser);
>use CGI::Carp qw(carpout);
>use strict;
>
>require "pl_lib.pl";
>
>$|=1;
>print "Content-type:text/html\\n\\n";
>
>my %FORM=read_input(); #-- to analyse the form data
>
>print <<eof;
> <p><h1>Thank you for your coming!!!</h1>
> name:$FORM{'name'}<p>
> addr:$FORM{'addr'}<p>
> tel:$FORM{'tel'}<p>
>eof
>
>print "C starts....<br>";
>
>my $val=qx(./add $FORM{'name'} $FORM{'addr'} $FORM{'tel'})||die $!;
>print "return value--$val<br>";
>
>print "C ends...<br>";
>
>--------------------------------
>The perl script can run correctly , so the problem is database....
>Could you help me again? Thank you~~~
>
>Peary
>
>
>
>
>
>
peary wrote: > > Hi, > > I use Linux with Informix SE, ESQL/C, ISQL 7.20. > I want to run my EC program on the web browser to insert/select data from > my informix server. I can run my EC program correctly and insert the data to > the database. But if I run it on the web then it cannot open the database, > the sqlca.sqlcode=-461 > Originally, i think maybe i didn't give the program the userid and pwd, > but after i tried it's the same..... > Here is my EC program "add.ec"..... This is usually because the WEB server is providing the runtime environment and it was not started with the Informix environment vars set. Make sure the script that starts the WEB server sets and exports: INFORMIXDIR DBPATH PATH (including $INFORMIXDIR/bin) LD_LIBRARY_PATH (including $INFORMIXDIR/lib & $INFORMIXDIR/lib/esql) INFORMIXSERVER Art S. Kagel
Hi, Jeepee,
Yes....that's why I cannot access database on the web ....
Someone told me I have to use "grant", so I changed my program to
$ connect to '/home/frm/tt@dbs_rm' ;
$ grant connect to public;
But it still don't work , do you know what's wrong?
Peary
> I think ./add is runned by user nobody. (apache and others default)
> Check ACLs.
>
> Jeepee
>
> peary a 嶰rit dans le message <7idn32$rik$1@news1.sinica.edu.tw>...
> >Hi,
> >
> >I use Linux with Informix SE, ESQL/C, ISQL 7.20.
> >I want to run my EC program on the web browser to insert/select data from
> >my informix server. I can run my EC program correctly and insert the data
> to
> >the database. But if I run it on the web then it cannot open the
database,
> >the sqlca.sqlcode=-461
> >Originally, i think maybe i didn't give the program the userid and pwd,
> >but after i tried it's the same.....
> >Here is my EC program "add.ec".....
> >-------------------------------------
> >#include <stdio.h>
> >#include <stdlib.h>
> >#include <string.h>
> >$include sqlca;
> >
> >void main(int argc,char *argv[]){
> > char name[20],addr[40],tel[10],item_list[1024+1];
> > $char sql_insert[1024+1],pwd[15+1];
> >
> > strcpy(pwd,"mypassword");
> >
> >$ connect to '/home/frm/tt@dbs_rm' user 'peary' using $pwd;> >
> >/* $ database 'tt@dbs_rm'; */
> >
> > if(sqlca.sqlcode<0){
> > printf("database open error!\\n");
> > exit(0);
> > }
> >
> > strcpy(name,argv[1]);
> > strcpy(addr,argv[2]);
> > strcpy(tel,argv[3]);
> >
> > sprintf(item_list,"('%s','%s','%s')",name,addr,tel);
> > sprintf(sql_insert,"%s %s\\n","insert into base
values",item_list);
> >
> >$ prepare cur from $sql_insert;
> >$ execute cur;
> >$ free cur;> >
> >$ disconnect current;> >
> > exit(1);
> >}
> >
> >------------------------------
> >I use "esql -o add add.ec" compiled it to "add", and if i run "./add 123
> 456
> >789"
> >under Linux, then it can connect to the database and insert the 3 datas.
> >But if I run it on the web( use perl to call ec program), then it always
> >returns
> >"database open error!".
> >Here is my perl program "tt.pl"...
> >------------------------------
> >
> >#!/usr/bin/perl
> >use CGI qw/:standard :html3/;
> >use CGI::Carp qw(fatalsToBrowser);
> >use CGI::Carp qw(carpout);
> >use strict;
> >
> >require "pl_lib.pl";
> >
> >$|=1;
> >print "Content-type:text/html\\n\\n";
> >
> >my %FORM=read_input(); #-- to analyse the form data
> >
> >print <<eof;
> > <p><h1>Thank you for your coming!!!</h1>
> > name:$FORM{'name'}<p>
> > addr:$FORM{'addr'}<p>
> > tel:$FORM{'tel'}<p>
> >eof
> >
> >print "C starts....<br>";
> >
> >my $val=qx(./add $FORM{'name'} $FORM{'addr'} $FORM{'tel'})||die $!;
> >print "return value--$val<br>";
> >
> >print "C ends...<br>";
> >
> >--------------------------------
> >The perl script can run correctly , so the problem is database....
> >Could you help me again? Thank you~~~
> >
> >Peary
> >
> >
> >
> >
> >
> >
>
>
peary wrote:
> Yes....that's why I cannot access database on the web ....
> Someone told me I have to use "grant", so I changed my program to
> $ connect to '/home/frm/tt@dbs_rm' ;
> $ grant connect to public;>
> But it still don't work , do you know what's wrong?
As Art Kagel said, it is all about environment.
However, the webservers do not pass on any inherited environment
to their sub-processes; they manufacture an almost completely empty
environment from scratch. If you need to get Informix environment
variables to the sub-processes, you have to ensure that Apache is
told to set them, either with PassEnv (which does relay the inherited
value) or with SetEnv - these appear in httpd.conf.
Additionally, you may be having problems with user ID; the web server
is often run as a non-privileged user. Unless you do otherwise,
though, your program will be unable to connect to the database,
so it won't be able to run the grant statement, etc. So, you have
to ensure that the web user can access the database.
You might care to look at DBI and DBD::Informix, and mod_perl.
There's also a HowTo in the Linux section at the IIUG web site:
http://www.iiug.org/
--
Jonathan Leffler (jleffler@informix.com, jleffler@earthlink.net)
Guardian of DBD::Informix v0.60 -- see http://www.perl.com/CPAN
#include <disclaimer.h>