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

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:

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

Create a Database

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

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”:

Create a Table

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

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”:

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:

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

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:

Display Record on Webpage

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

Syntax

This code display all record in decending order.

display-record.php

Display Record

 

Leave a Reply

Your email address will not be published. Required fields are marked *