On Mon, 30 Apr 2001 08:03:23 +0200, justin todd said:

> Hi all. 
>  I am in desparate need of advice.
>  I am running a NT4.0/IIS server and a MSSQL7 database. I ahve got Perl
>  working fine on the Web Server but I cannot perform simple function on
>  the Database. Below is my code and the different error's I recieve when
>  I alter the code. I hope it makes sence. One of the problem's is that I
>  am not 100% sure that I am connecting to the DB. Before I was getting
>  Connect errors and then I changed the connect string and they stopped so
>  I can only asume no errors means no problems, Ok you can stop laughing
>  now. 
>  Anyway Please help
>  Kind Regards 
>  Justin
>  

There are a couple of problems with your script. I'll go through
it line by line:


>  
>  Sript
>  #!/usr/local/bin/perl5
>  use DBI;
>  $dbh = DBI.connect("DBI:ODBC:database=**", "**");
>  #print "Content-Type: text/html\n\n";    

You want to call the method connect from the package (class)
DBI. This is done with -> operator and _not_ with the dot.
(The dot dot is string concatenation).
What you want to do is:

$dbh = DBI->connect("DBI:ODBC:database=**", "**");

Now you have a database connection in $dbh.

>  
>  #$SQL="select name from test";

The hash comments the line out. So $SQL contains nothing.
So what is the database to do? Leave that line without the hash.

>  $dbh->do($SQL);

The do(...) Method just executes the SQL-Statement on the database
and returns if it succeded or not. This is not what you want. You
want the data from the database. You are preparing and fetching
further down. This is the right way to do a select. You can just
delete the "do"-line.


>  $sth = $dbh->prepare($SQL);
>  $sth->execute();
>  while (($boy) = $sth->fetchrow) 
>  {
>      print "debug $boy";
>  }
>  $sth->finish();
>  $dbh->disconnect();

This should work now. In the loop $boy contains the name-value for
one line from your test-table.

>  ###################################################
>  CGI Error
>  The specified CGI application misbehaved by not returning a complete set
>  of HTTP headers. The headers it did return are: 
>  Can't locate auto/DBI/do.al in @INC (@INC contains: C:/Perl/lib
>  C:/Perl/site/lib .) at D:\Web_Clients\test\cgi-bin\db.pl line 20
>  
>  ######################################################################
>  
>  
>  Then with no hash by the $SQL="select ......" same error
>  
>  Then with line 16 "Content-type etc"  not hashed I just a get a blank
>  page and when I try view the source nothing comes up.
>  
>  


Now you should be able to run the script from the commandline.
Just type perl scriptname.pl on the DOS-Prompt.

The reason you cannot run it yet as a CGI-Script is, that a
CGI-Script has to follow certain rules fro writing text.
For starters it has to send a HTTP-Header to the Browser.
This is achieved very easily with the Module CGI.
You should look at its documentation (perldoc CGI).

But to give you a start:
Add this after your use DBI; line:

use CGI qw/:standard/;

print header;




Hope that helps (otherwise ask again),

cr

Reply via email to