Unit 7: Working with Databases

4th semester

Unit 1 Unit 2 Unit 3 Unit 4 Unit 5 Unit 6 Unit 8 Unit 9
Unit 10

What is MySQL?

  • MySQL is a a database.
  • The data in MySQL is stored in database objects called
  • A table is a collections of related data entries and it consists of columns and
  • Databases are useful when storing information categorically. A company may have a database with the following tables: “Employees”, “Products”, “Customers” and “Orders”.

Connect to Database

mysql_connect() function is used to connect php code with MySQL database. It returns resource if connection is established otherwise null.
Syntax

mysql_connect(servername,username,password);
Parameter Description
servername Optional. Specifies the server to connect to. Default value is “localhost:3306”
username Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process
password Optional. Specifies the password to log in with. Default is “”

Example
 In the following example we store the connection in a variable ($con) for later use in the script. The “die” part will be executed if the connection fails:

<?php
$con = mysql_connect("localhost","peter","abc123"); if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?>

Closing a Connection

mysql_close() function function is used to disconnect php code with MySQL database. It returns true if connection is closed otherwise false

<?php
$con = mysql_connect("localhost","peter","abc123"); if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code mysql_close($con);
?>

Create a Database

The CREATE DATABASE statement is used to create a database in MySQL.
Syntax

CREATE DATABASE database_name

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.
Example
 The following example creates a database called “my_db”:

<?php
$con = mysql_connect("localhost","peter","abc123"); if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysql_close($con);
?>

Create a Table

The CREATE TABLE statement is used to create a table in MySQL.
Syntax

CREATE TABLE table_name (
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
)

We must add the CREATE TABLE statement to the mysql_query() function to execute the command.
Example
The following example creates a table named “Persons”, with three columns. The column names will be “FirstName”, “LastName” and “Age”:

<?php
$con = mysql_connect("localhost","peter","abc123"); if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
// Create table mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Persons (
FirstName varchar(15), LastName varchar(15), Age int
)";
// Execute query mysql_query($sql,$con);
mysql_close($con);
?>

Important: A database must be selected before a table can be created. The database is selected with the mysql_select_db() function.
Note: When you create a database field of type varchar, you must specify the maximum length of the field, e.g. varchar(15).

Insert Data Into a Database Table

 The INSERT INTO statement is used to add new records to a database table.
Syntax
It is possible to write the INSERT INTO statement in two forms.The first form doesn’t specify the column names where the data will be inserted, only their values:

INSERT INTO table_name
VALUES (value1, value2, value3,...)

The second form specifies both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

To get PHP to execute the statements above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.
Example
 In the previous chapter we created a table named “Persons”, with three columns; “Firstname”, “Lastname” and “Age”. We will use the same table in this example. The following example adds two new records to the “Persons” table:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire', '33')");
mysql_close($con);
?>

Display Record on Webpage

To display all record from database on webpage; we write below mysql syntax

Syntax

<?php
include 'db-connect.php';
mysql_select_db("tutorohx_rw", $conncection);
$sql = "SELECT * from user ORDER BY s_no DESC";
$result = mysql_query($sql, $connection);
?>

This code display all record in decending order.

display-record.php

Display Record

<table>
<tr><th>ID</th><th>First Name</th><th>Last Name</th></tr>
<tr>
<td><?php echo $row["f_name"]; ?></td>
<td><?php echo $row["l_name"]; ?></td>
</tr>
<?php
if ($result)
{
while($row = mysql_fetch_array($result))
{
?>
<?php
}
}
?>
</table>

 

Leave a Reply

Your email address will not be published.