Connecting to MySQL database via Perl/CGI on Godaddy Linux Hosting

Connecting to MySQL database via Perl/CGI on Godaddy Linux Hosting

I have had to face the problem of finding the right syntax to connect to MySQL server on my linux hosting at godaddy. Finally after days of searching, and no help from godaddy support (they suck straws big time!), I was able to finally get my database to connect and proceed further.

This is the script:



Explaination line-by-line:

Line 1:  #<some text>    ---- > this is a comment, but
#!<some path> ----> this is required for your script to work, it tells where the perl files are located. 
 #!/usr/bin/perl is saying that perl files are located in a local folder, which is located at /user/bin/perl  

Line 2: Apparently with Godaddy, modules only load when you add this additional line (use cPanelUserConfig;) Remember, that Perl uses a module called DBI to connect to databases of various types, it is later on described, which type of database it is, but this line only says where to find or load the modules location.

Line 3: Use CGI, I'm not 100% about this, but I think it is important for the script to work. The file I saved was as a .fpl file. While pl is the usual Perl file, making it fpl on godaddy hosting, uses the FCGI instead of normal CGI.

FCGI is the way to go, and is ideal, since without much changes to the CGI script, the same script works as much as 10 times faster. Google FCGI for more info, but that is what it is all about in few words.

Line 4: That is the standard line, that makes your script display any html outputs, without it your script might not give the desired result. But I guess you already know about that line, and its use before reading this guide.

Line 5: Now we mention, that we have to use the module DBI, to connect to databases. In line 2 we only had told the program, where to find the modules, but not the ones to use. 

Line 6: Now we are ready to make the connection to the database. This is the core of what you might be searching, so pay attention to only replace, the following with relevant values:
NameofDatabase  
NameofUser
passwordofUser

note, that this is not a very secure way of connecting to a database, and you must search further or devise one. One way to do it is, since the file here is readable/executable by anyone in the world, its better to make another file, where these 3 vital values should not be in clear text, and that file should be readable & executable only by localuser. For now be content with this script, since you are getting started.

Line 7: Says, throw a error (a code 500 error) if fails to connect to database.

Line 8 to 12: This is a sample query example I placed, to tell me if my connection was made or not, and if everything is working fine or not. Anything you want to do in your database, must be after line 7 and before the line $dbh->disconnect(); <--- this is where you are disconnecting from the database at the end of the script, as a basic security measure.

If all goes well, you should be seeing this text at the top left corner after you execute the script from the outside:

Deleted 1 row 

_________________________________________________________________________________

Additional notes:

- You might have to install a few modules, and submodules by going to your cpanel, and then perl modules. 

- It is absolutely necessary to put the permissions of the file as 705 or 755. That you can do by going to 
www/cgi-bin folder (where you should be keeping all your .fpl/.cgi/.pl files) and right click on the relavent file, and change permissions. 

- I found this, thanks to this website: http://sql-info.de/mysql/examples/Perl-DBI-examples.html

- You may show your appreciation, by clicking on some ads (that is if you have no adblocker programs active in your browser). That gives us some ad revenue from google ads. 

Bye again! 

Comments

Popular posts from this blog

Shared Displays

Amazon loses Debit, All credit to Visa Debit

Mega Vs Dropbox Vs Boxnet