Steps to be followed
1) Always begin with the following commands
# service httpd start
or
# service network start
2) To check whether apache server is running in the background
# service httpd status
3) All the HTML, XML, XSLT, CSS, PHP programs should be saved in
# cd /var/www/html
4) Open the Editor and type the program.
5) To run perl programs ( All perl programs should be saved under .pl extension)
# cd /var/www/cgi-bin
6) Change the file permissions to executable mode
# Chmod 0777 [Link]
7) To see the output go to browser and in the address bar type [Link]
8) For mysql programs
# service mysqld start
# mysql
mysql> show databases;
(use any of the databases available or create a new database )
mysql>create database student
mysql>use student
show tables
If tables are not available create a new table by the following query statement
create table tablename + attributes
1. Write a Perl program to insert name and age information entered
by the user into a table created using MySQL and to display the
current contents of this table.
#! /usr/bin/perl
print "Content-type: text/html\n\n";
print "<HTML><TITLE>Result of the insert operation </TITLE>";
use CGI ':standard';
use DBI;
$dbh=DBI->connect("DBI:mysql:satish","root","ghalige");
$name=param("name");
$age=param("age");
$qh=$dbh->prepare("insert into stud values('$name','$age')");
$qh->execute();
$qh=$dbh->prepare("Select * from stud");
$qh->execute();
print "<table border size=1><tr><th>Name</th><th>Age</th></tr>";
while ( ($name,$age)=$qh->fetchrow())
{
print "<tr><td>$name</td><td>$age</td></tr>";
}
print "</table>";
$qh->finish();
$dbh->disconnect();
print"</HTML>";
<html>
<body>
<form action="[Link]
Name : <input type="text" name="name"> <br>
Age :<input type="text" name="age"> <br>
<input type="submit" value="Submit">
</form>
</html>
2. Create a XHTML form with Name, Address Line 1, Address Line 2,
and E-mail text fields. On submitting, store the values in MySQL
table. Retrieve and display the data based on Name.
<html>
<body>
<?php
<-- [Link] -->
$dbh = mysql_connect('localhost', 'root', '') or
die(mysql_error());
mysql_select_db('mca') or die(mysql_error());
if(isset($_POST['name']))
{
$nme = $_POST['name'];
$ad1 = $_POST['add1'];
$ad2 = $_POST['add2'];
$eml = $_POST['email'];
if($nme != "" && $ad1 != "")
{
$query = "INSERT INTO contact VALUES
('$nme', '$ad1', '$ad2', '$eml')";
echo Record inserted Successfully;
$result = mysql_query($query) or die(mysql_error());
}
else
echo "one of the field is empty";
}
mysql_close($dbh);
?>
<FORM ACTION="[Link]" METHOD="POST">
<P>
Name: <INPUT TYPE=text NAME="name" value=""> <BR>
Address 1:<INPUT TYPE=text NAME="add1" value=""><BR>
Address 2:<INPUT TYPE=text NAME="add2" value=""><BR>
email:<INPUT TYPE=text NAME="email" value=""><BR>
<INPUT TYPE=submit>
</FORM>
</body>
</html>
<html>
<!-- [Link] -->
<head><title>Program 12</title></head>
<body>
<form action="[Link]" method="post">
Enter Name of the person <input type="text" name="name">
<input type=submit>
</form>
</body>
</html>
<html>
<!-- [Link] -->
<head><title>Search Result </title></head>
<body>
<h3>Search Result </h3>
<hr>
<?php
$link=mysql_connect("localhost","root","");
mysql_select_db("mca");
$n=$_POST["name"];
print "Entered Name is $n \n";
$var=mysql_query("SELECT * FROM contact WHERE name like '%$n
%'");
echo"<table border size=1>";
echo"<tr><th>Name</th> <th>Address 1</th> <th>Address 2</th>
<th>E-mail</th></tr>";
while ($arr=mysql_fetch_row($var))
{
echo "<tr><td>$arr[0]</td> <td>$arr[1]</td> <td>$arr[2]</td>
<td>$arr[3]</td> </tr>";
}
echo"</table>";
mysql_free_result($var);
mysql_close($link);
?>
<hr>
<form action="[Link]">
<input type="submit" value="Back">
</form>
</body>
</html>
3. Write a PHP program to read student data from an XML file and
store into the MYSQL database. Retrieve and display.
<?php
[Link]
$xmlstr = <<<XML
<? Xml version=1.0 standalone=yes?>
<student_details>
<student>
<stud_name> ABCD </stud_name>
<stud_usn> 1234 </stud_usn>
<stud_branch> MCA </stud_branch>
</student>
<student>
<stud_name> EFGH </stud_name>
<stud_usn> 5678 </stud_usn>
<stud_branch> MBA </stud_branch>
</student>
</student_details>
XML;
?>
-------------------------------------------------------------<?php
[Link]
include [Link];
$xml = new SimpleXMLElement($xmlstr);
echo $xml->getName() . <br />;
echo multiple values: <br />;
$link=mysql_connect(localhost,root,);
mysql_select_db(mca);
foreach ($xml->student as $student)
{
echo $student->stud_name . <br />;
echo $student->stud_usn . <br />;
echo $student->stud_branch . <br />;
echo <br />;
$t=mysql_query(insert into student values($student>stud_name,$student->stud_usn,$student->stud_branch));
}
-------------------------------------------------------------<html> <head>
[Link]
<title> A program to get values from database
</title></head>
<body>
<form action=[Link] method=post>
Enter Name of the person
<input type=text name=name />
<input type=submit value=submit />
</form> </body></html>
<![Link]-->
<html> <head><title> Search Result </title></head> <body>
<h3> Search Record </h3>
<hr>
<?php
$link=mysql_connect(localhost,root,);
mysql_select_db(mca);
$n=$_POST[name];
print entered name is $n \n;
$var=mysql_query(select * from student where stud_name like
%$n%);
echo <table border size=1>;
echo <tr><th>StudentName</th><th>Student_USN</th><th>Student-Branch</th></tr>;
while ($arr=mysql_fetch_row($var))
{
echo <tr><td>$arr[0]</td><td>$arr[1]</td><td>$arr[2]</td>
</tr>;
}
echo </table>;
mysql_free_result($var);
mysql_close($link);
?>
<hr>
<form action=[Link]>
<input type=submit value=Back>
</form></body></html>
4. Build a rails application to accept book information viz. Accession
number, title, authors, edition and publisher From a web page and
store the information in a database And to search for a book with
the title specified by the User and to display the search results with
proper
Headings.
1) rails -d mysql lab12
2) cd lab12
3) mysql -u root
4) create database lab12_development;
create database lab12_test;
create database lab12_production;
5) use lab12_development;
6) create table books (
id int not null auto_increment,
name varchar(80) not null,
description text not null,
price decimal(8, 2) not null,
primary key(id) );
7) ruby script/generate scaffold Book name:string description:text price:float
8) ruby script/generate controller main
//This program has to be typed in /rails_apps/apps/controllers/main(notepad
file)
9) class MainController < ApplicationController
def welcome
@num_books = [Link]
end
def result
@bookid = params[:sid]
@bookz = [Link](:all, :conditions => "id = #{@bookid}")
end
end
// Store this .rhtml file in /rails_apps/apps/views/[Link](notepad
file)
10) <html>
<title> Welcome template for books </title>
<body>
<p> Entered book id is <%= @bookid %>
</p>
<table border=1>
<tr><th>Book Id</th><th>Book Name</th><th>Details </th> <th>Price </th>
</tr>
<% @[Link] do |bk|
@id = [Link]
@name = [Link]
@descp = [Link]
@price = [Link] %>
<tr>
<td> <%= @id %></td>
<td><%= @name %> </td>
<td><%= @descp %></td>
<td> <%= @price %></td>
/tr>
<% end %>
</table> </body></html>
// Store this .rhtml file in /rails_apps/apps/views/[Link] (notepad
file)
11) <html>
<title> Welcome template for books </title>
<body>
<p> Total number of books = <%= @num_books %> </p>
<form action = "filename1" >
Enter Searching Element: <input type="text" name="sid" />
<input type=submit value="Search" />
</form>
</body>
</html>
12) ruby script/server
13) [Link]
OUTPUT: