473,750 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Connecting PERL to a SQL Server Database

35 New Member
Greetings all.

I have a problem with PERL and SQL Server.

I need to upload a database query to a web page using PERL and SQL Server. I have tried to do this myself but nothing displays on the page. The .CGI file will call the PERL but does not display any of the results of the query search via SQL Server on my page. I have commented out the output I wanted because this causes an Internal Server Error.

I realise that this is a very basic PERL Script. I do struggle with PERL but i do urgently need to create this connection.

Below is the CGi script I am using to Call the page and display the result. Usernames and passwords have been edited to aid ambiguity.

I do hope someone can help. Thank you,

Jonnie

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. #include the Perl libraries for connecting to databases
  4. use DBI;
  5.  
  6. #state that you will be using the CGI package for webpages in Perl
  7. use CGI;
  8.  
  9. #state that text after a print statement will be in HTML
  10. print "Content-type: text/html\n\n";
  11.  
  12. #start the HTML part of the page and create the title bar
  13. print "<html>";
  14. print "<head>";
  15. print "<title>SQL Server Testing of ???.SQL</title>";
  16. print "</head>";
  17.  
  18. #start the body of the HTML part of the page
  19. print "<body>";
  20.  
  21. #connect to the database
  22. #use the statement below, substituting u0021646 for your user id and your_password for your SQL Server password.
  23. #blah blah is the database server used.
  24. $dbh = DBI->connect("*******","*****","*****");
  25.  
  26. #Select the database to be used (your user id)
  27. $dbh->do("clergy");
  28.  
  29. #specify the records to be selected
  30. $sql="SELECT clergy_code, clergy_name, age FROM clergy WHERE (age>30)";
  31. #$sql="SELECT *";
  32. #prepare the query
  33. $sth=$dbh->prepare ($sql);
  34.  
  35. #execute the query
  36. $sth->execute;
  37.  
  38. #get the results a record at a time
  39. while(($clergy_code, $clergy_name, $age)=$sth->fetchrow_array){
  40. #print results
  41. print "clergy_code: ".$clergy_code. "clergy_name: ".$clergy_name. "age: ".$age."\n";
  42. print "<br />";
  43. };
  44.  
  45. #<font face = "arial" color = "blue" size = "2">Data one is $clergy_code, Data two is $clergy_name, Data #three is $age.</font>
  46.  
  47. #print "<table border = "1">";
  48. #print "<tr>";
  49. #print "<th>Clergy Code</th>";
  50. #print "<th>Clergy Name</th>";
  51. #print "<th>Age</th>";
  52. #print "</tr>";
  53. #print "<tr>";
  54. #print "<td>.$clergy_code.</td>";
  55. #print "<td>.$clergy_name.</td>";
  56. #print "<td>.$age.</td>";
  57. #print "</tr>";
  58. #print "</table>";
  59.  
  60. print "</body>";
  61.  
  62. #sources include George Cormack
  63. #helpdesk/user_guides/perl_sql1.html
  64.  
  65. #finish the query and reclaim the memory used
  66. $sth->finish;
  67.  
  68. #disconnect from the database
  69. $dbh->disconnect;
  70.  
Apr 1 '07 #1
4 4155
miller
1,089 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. $dbh = DBI->connect("*******","*****","*****");
  2.  
  3. #Select the database to be used (your user id)
  4. $dbh->do("clergy");
  5.  
  6. $sql="SELECT clergy_code, clergy_name, age FROM clergy WHERE (age>30)";
  7. $sth=$dbh->prepare ($sql);
  8. $sth->execute;
  9.  
Hello Jonnie,

Your problems are entirely in the above set of code. First of all, you should always include error messages in your database operations. We all create syntax errors, so do yourself a favor and let dbi tell you about them. Secondly, your method of selecting a database appears malformed. Unfortunately, I only use MySQL, so I can't know for sure if the syntax is wrong. But I would suggest that you include the database you're going to use in the actual connect string instead of a later statement.

Expand|Select|Wrap|Line Numbers
  1. my $dbname = 'clergy';
  2. my $dbuser = 'fooUSER'
  3. my $dbpass = 'fooPASS'
  4. my $dbhost = 'localhost';
  5.  
  6. $dbh = DBI->connect("dbi:mysql:$name:$host", $user, $pass) or die "Database connection failed.";
  7.  
  8. my $sql = "SELECT clergy_code, clergy_name, age FROM clergy WHERE (age>30)";
  9. $sth = $dbh->prepare($sql);
  10. $sth->execute or die $dbh->errstr;
  11.  
You will probably have to edit the value mysql to somethign more appropriate, but this coding style should enable you to better figure out what is wrong with your setup.

Good Luck,
- Miller
Apr 1 '07 #2
jonniethecodeprince
35 New Member
Thank you Miller miller.

Unfortunately I have tried everything I can think of to try and make this connection work. I cannot do so.

My preference is to use PERL and SQL server. This is something that I have never managed to do before.

I would hope that someone with an experience of using SQL can combine their knowledge of SQL combined with all I know of PERL to provide that killer script that I am looking for.

If not, I can try another way maybe using Access (this is a step back I know) and Javascript to connect to a database that way. At least then I can use what time I have left to create something.

Many thanks for all your help everyone.

Jonnie
Apr 14 '07 #3
KevinADC
4,059 Recognized Expert Specialist
You said you commented out the part you wanted because it causes an internal server error, that is becuase you have strings like this:

Expand|Select|Wrap|Line Numbers
  1. print "<table border = "1">";
You have to escape double-quotes within a double-quoted string:

Expand|Select|Wrap|Line Numbers
  1. print "<table border =\"1\">";
or use a quoting operator that does it for you:

Expand|Select|Wrap|Line Numbers
  1. print qq~<table border = "1">~;
where '~' is an arbitrary delimter that tells perl where the 'q' or 'qq' operator begins and ends. You could use a different delimiter:

Expand|Select|Wrap|Line Numbers
  1. print q!<table border = "1">!;
  2. print qq#<table border ="$foo">#;
  3. print q(<table border = "1">);
  4. print q[<table border = "1">];
  5.  

note that brackets need to be used as a pair and not as a single character
Apr 14 '07 #4
jonniethecodeprince
35 New Member
Hello everyone.

I'm sorry I haven't been replying much to this thread in recent days. Obviously i've been racking my brains these past weeks working out how to resolve this problem.

I thought I found the answer when I changed tac and tried to connect the same database, only with MS Access, JavaScript and ASP. The truth is I am still learning all of these aspects of programming although I am quite familiar with Access but haven't even touched PHP. .

I now have a month to get a connection going. Gonna need lotsa luck :)
Apr 24 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2685
by: christian_stengel | last post by:
Hi *, I have just started to learn python and I am having a problem with an python client connecting to a perl server using ssl (I tried this with pyOpenSSL and with the build in SSL Module). I don't want to check a cerificate, so i simply tried a from OpenSSL import SSL
12
2789
by: Ann Marinas | last post by:
Hi all, I would like to ask for some help regarding separating the asp.net webserver and the sql server. I have created an asp.net application for a certain company. Initially, we installed both the iis and sql server in a single machine. Not too long ago, the machine had some hardware problems, and management has decided to purchase new servers, for both asp.net and sql server.
1
2354
by: mm | last post by:
I have several korn shell scripts I use with a MySQL database on the same server (Solaris). I am moving to a Linux environment where the MySQL database is on another server. I would like to convert the database connection code to connect to the database on another Linux server. I see alot of information on Perl DBI CLI and PHP DBI CLI but cannot find any examples of Korn Shell connecting to a database on another server. Is there...
5
1885
by: kriz4321 | last post by:
I have perl(version 5.004) Installed in my unix server. I need to connect to SQL Database via perl. Can you tell me what modules I should Install and how to make a test connection to the same. What version of DBI and DBD::ODBC should be Installed for the this? When I checked this site 'http://ppm.activestate.com/PPMPackages/zips/6xx-builds-only/" there are many modules corresponding to DBI.. confused in choosing the right one..
3
6829
by: hakiran | last post by:
Hello all, I have been using Perl DBI the last 6months or so. I use it extensively with MySQL. But recently i tried to access Oracle DB with it and was having trouble. Any help would be appreciated. Here is the code and the error i get. I know the table/view do exist. Thanks all Kiran ----------
2
4216
by: bhusandi | last post by:
Dear All, I am new to this forum and perl ..;) I am trying to connect to Sybase (windows env) but am getting the error: "install_driver(Sybase) failed: Can't locate loadable object for module DBD::Syba se in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib .) at (eval 1) line 3 Compilation failed in require at (eval 1) line 3. Perhaps a module that DBD::Sybase requires hasn't been fully installed" Here is the code:
0
1392
by: Rebles | last post by:
I'm writing a PERL script to access and insert rows into a Microsoft SQL. i'm using MS SQL Server Management Studio Express (2005) to architect tables and queries. I've inserted two records into my "Pools" table One from MS SQL Manager as a test, and another from my PERL script. The one inserted by MS SQL Manager shows up everytime. The row from my PERL script doesn't show up on MS SQL Manager and ONLY shows up on my PERL script and...
1
2279
by: Rebles | last post by:
Hi, I just posted this in the MS SQL Section, but maybe my problem is rooted in Perl, so it's more appropriate to post here instead (sorry for the double post) I'm writing a PERL script to access and insert rows into a Microsoft SQL. i'm using MS SQL Server Management Studio Express (2005) to architect tables and queries. I've inserted two records into my "Pools" table One from MS SQL Manager as a test, and another from my PERL script....
2
5061
by: bobt1991 | last post by:
I just installed Microsoft Visual Studio 2008 Express edition, and it installs some sort of stripped down version of SQL Server. It comes with no tools for connecting to your "local" database, creating tables, etc., but it does allow you to create database files in SDF format within the IDE. My question is: How do you then connect to these database files in Perl? I tried creating an ODBC connection, but there seems to be no way to tie an...
0
9001
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9397
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9344
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8264
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.