Unit 5
Unit 5
Unit 5
V
ESTABLISHING A DATABASE
CONNECTION
&
WORKING WITH DATABASE
5.1 Overview of
Database
• Database is a collection of organized information, so that it can be
easily accessed, managed and updated.
• Thus you can say database is collection of one or more tables which are
related with one another.
• A table in database used to store records. The records in the table are
organized in the form or rows. Each row in the table represents
particular record.
• It can run on almost all platforms such as Linux, Unix and Windows.
• It can work with many programming languages such as PHP, C, C++, JAVA,
PERL etc.
• PHP provides various built in functions that allow you to use MySQL
commands from PHP page. Thus you can integrate PHP with MySQL.
• Following are the frequently used PHP functions that allows the execution
of MySQL commands:
– mysql_error(): Shows the error message that has been returned directly
from the MySQL server.
5.3 Creating Database using PHPMyAdmin
& Console
Covered in
Laboratory
5.4 Connecting with Database , Creating
& executing queries using
mysql_query()
• 5.4.1 Connecting with Database
• Before you start working with MySQL database server, you first have to
establish connection with MySQL server.
– Servername: Indicates name of the MySQL server with which you want
to
establish connection.
– username: Indicates name of the user using which you can logs on to
MySQL server.
– password: Indicates password of the user using which you can logs on
to MySQL server.
• Example:
$con = mysql_connect(“localhost”,
if(res)
{
echo “Table Created Successfully.”;
}
else
{
echo “Error while executing query.”;
}
5.5 Creating table, inserting data in to
table through HTML Forms
• 5.5.1 Creating table
<?php
if(res)
{
echo “Table Created Successfully.”;
}
else
{
echo “Error while executing query.”;
}
5.5 Creating table, inserting data in to
table through HTML Forms
<?php
$enroll = $_POST["enroll"];
$name = $_POST["fname"];
$sem = $_POST["sem"];
$dep = $_POST["dep"];
$con); mysql_close($con);
<?php
if(mysql_num_rows($res)>0)
{
while ( $row = mysql_fetch_array($res))
{
echo “<tr> <td> $row[Enrollment]
</td> <td> $row[Name]
</td> <td> $row[Semester] </td> <td> $row[Department]
</td>
</tr>” ;
}
echo “</TABLE>”;
mysql_close($con);
*******