Server Side Programming Articles - Page 2063 of 2650

First CGI Program using Perl

Mohd Mohtashim
Updated on 02-Dec-2019 08:01:54

434 Views

Here is a simple Perl CGI program available in file called hello.cgi. This file has been kept in /cgi-bin/ directory and it has the following content. Before running your CGI program, make sure you have change mode of file using chmod 755 hello.cgi UNIX command.#!/usr/bin/perl print "Content-type:text/html\r\r"; print ''; print ''; print 'Hello Word - First CGI Program'; print ''; print ''; print 'Hello Word! This is my first CGI program'; print ''; print ''; 1;Now if you click hello.cgi link then request goes to web server who search for hello.cgi in /cgi-bin directory, execute it and whatever result got generated, web server sends that result ... Read More

Useful DBI Functions in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 08:00:02

156 Views

Checking available_drivers@ary = DBI->available_drivers; @ary = DBI->available_drivers($quiet);Returns a list of all available drivers by searching for DBD::* modules through the directories in @INC. By default, a warning is given if some drivers are hidden by others of the same name in earlier directories. Passing a true value for $quiet will inhibit the warning.Checking installed_drivers%drivers = DBI->installed_drivers();Returns a list of driver name and driver handle pairs for all drivers 'installed' (loaded) into the current process. The driver name does not include the 'DBD::' prefix.Checking data_sources@ary = DBI->data_sources($driver);Returns a list of data sources (databases) available via the named driver. If $driver is ... Read More

Using NULL Values in Perl Database Operation

Mohd Mohtashim
Updated on 02-Dec-2019 07:57:00

1K+ Views

Undefined values, or undef, are used to indicate NULL values in Perl’s Database Operations. You can insert and update columns with a NULL value as you would a non-NULL value. These examples insert and update the column age with a NULL value −$sth = $dbh->prepare(qq {    INSERT INTO TEST_TABLE (FIRST_NAME, AGE) VALUES (?, ?) }); $sth->execute("Joe", undef);Here qq{} is used to return a quoted string to prepare API. However, care must be taken when trying to use NULL values in a WHERE clause. Consider −SELECT FIRST_NAME FROM TEST_TABLE WHERE age = ?Binding an undef (NULL) to the placeholder will ... Read More

COMMIT & Rollback Operations in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:56:02

1K+ Views

COMMIT OperationCommit is the operation which gives a green signal to database to finalize the changes and after this operation no change can be reverted to its orignal position.Here is a simple example to call commit API.$dbh->commit or die $dbh->errstr;ROLLBACK OperationIf you are not satisfied with all the changes or you encounter an error in between of any operation, you can revert those changes to use rollback API.Here is a simple example to call rollback API.$dbh->rollback or die $dbh->errstr;Begin TransactionMany databases support transactions. This means that you can make a whole bunch of queries which would modify the databases, but none of the ... Read More

Database DELETE Operation in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:54:26

866 Views

Perl DELETE operation is required when you want to delete some records from your database. Following is the procedure to delete all the records from TEST_TABLE where AGE is equal to 30. This operation will take the following steps.Preparing SQL query based on required conditions. This will be done using prepare() API.Executing SQL query to delete required records from the database. This will be done using execute() API.Releasing Stattement handle. This will be done using finish() API.If everything goes fine then commit this operation otherwise you can rollback complete transaction.$age = 30; my $sth = $dbh->prepare("DELETE FROM TEST_TABLE WHERE AGE = ?"); $sth->execute( $age ) or ... Read More

Database UPDATE Operation in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:52:37

1K+ Views

Perl UPDATE Operation on any database means to update one or more records already available in the database tables. Following is the procedure to update all the records having SEX as 'M'. Here we will increase AGE of all the males by one year. This will take three steps −Preparing SQL query based on required conditions. This will be done using prepare() API.Executing SQL query to select all the results from the database. This will be done using execute() API.Releasing Statement handle. This will be done using finish() API.If everything goes fine then commit this operation otherwise you can rollback complete transaction. See next ... Read More

Database READ Operation in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:51:02

752 Views

Perl READ Operation on any databasse means to fetch some useful information from the database, i.e., one or more records from one or more tables. So once our database connection is established, we are ready to make a query into this database. Following is the procedure to query all the records having AGE greater than 20. This will take four steps −Preparing SQL SELECT query based on required conditions. This will be done using prepare() API.Executing SQL query to select all the results from the database. This will be done using execute() API.Fetching all the results one by one and ... Read More

Database INSERT Operation in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:48:46

2K+ Views

Perl INSERT operation is required when you want to create some records into a table. Here we are using table TEST_TABLE to create our records. So once our database connection is established, we are ready to create records into TEST_TABLE. Following is the procedure to create single record into TEST_TABLE. You can create as many as records you like using the same concept.Record creation takes the following steps −Preparing SQL statement with INSERT statement. This will be done using prepare() API.Executing SQL query to select all the results from the database. This will be done using execute() API.Releasing Stattement handle. ... Read More

How to create Database Connection in Perl?

Mohd Mohtashim
Updated on 02-Dec-2019 07:45:13

454 Views

Assuming we are going to work with MySQL database with Perl. Before connecting to a database make sure of the followings. You can take help of our MySQL tutorial in case you are not aware about how to create database and tables in MySQL database.You have created a database with a name TESTDB.You have created a table with a name TEST_TABLE in TESTDB.This table is having fields FIRST_NAME, LAST_NAME, AGE, SEX and INCOME.User ID "testuser" and password "test123" are set to access TESTDB.Perl Module DBI is installed properly on your machine.You have gone through MySQL tutorial to understand MySQL Basics.Following ... Read More

Destructors and Garbage Collection in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:43:59

465 Views

If you have programmed using object oriented programming before, then you will be aware of the need to create a destructor to free the memory allocated to the object when you have finished using it. Perl does this automatically for you as soon as the object goes out of scope.In case you want to implement your destructor, which should take care of closing files or doing some extra processing then you need to define a special method called DESTROY. This method will be called on the object just before Perl frees the memory allocated to it. In all other respects, the ... Read More

Advertisements