Advanced IP-Chapter-4 - Lect-8, 9
Advanced IP-Chapter-4 - Lect-8, 9
Chapter 4 :
Manipulating MySQL
Databases with PHP
What is Database?
2
and indexes.
Guarantees the Referential Integrity between rows of various
tables.
Updates the indexes automatically.
Interprets an SQL query and combines information from
various tables.
RDBMS Terminology:
4
Before we proceed to explain MySQL database system,
lets revise few definitions related to database.
Database: A database is a collection of tables, with
related data.
Table: A table is a matrix with data. A table in a database
looks like a simple spreadsheet.
Column: One column (data element) contains data of
one and the same kind, for example the column postcode.
Row: A row (= tuple, entry or record) is a group of
related data, for example the data of one subscription.
Redundancy: Storing data twice, redundantly to make
the system faster.
cont...
5
Primary Key: A primary key is unique. A key value can not
occur twice in one table. With a key you can find at most one
row.
Foreign Key: A foreign key is the linking pin between two
tables.
Compound Key: A compound key (composite key) is a key
that consists of multiple columns, because one column is not
sufficiently unique.
Index: An index in a database resembles an index at the back
of a book.
Referential Integrity: Referential Integrity makes sure that a
foreign key value always points to an existing row.
MySQL Database:
6
specified then default is the name of the user that owns the
server process.
passwd
Optional - The password of the user accessing the database. If
database is closed.
This function returns true if it closes connection
Syntax:
bool mysql_select_db( db_name, connection );
db_name
Required - MySQL Database name to be selected
connection
Optional - if not specified then last opened connection
by mysql_connect will be used.
Here is the example showing you how to select a database.
23
<html><head><title>Selecting MySQL
Database</title>
</head><body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'guest';
$dbpass = 'guest123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
cont...
24
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db( 'TUTORIALS' );
mysql_close($conn);
?>
</body> </html>
NOTE:
25
Although numeric and date types are fun, most data you'll
store will be in string format. This list describes the
common string datatypes in MySQL.
CHAR(M) - A fixed-length string between 1 and 255
characters in length (for example CHAR(5)), right-
padded with spaces to the specified length when stored.
Defining a length is not required, but the default is 1.
VARCHAR(M) - A variable-length string between 1 and
255 characters in length; for example VARCHAR(25).
You must define a length when creating a VARCHAR
field.
cont...
33
Syntax:
Here is generic SQL syntax to create a MySQL
table:
CREATE TABLE table_name (column_name
column_type);
cont...
36
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br />';
$sql = "CREATE TABLE tutorials_tbl( ". "tutorial_id INT NOT NULL
AUTO_INCREMENT, ". "tutorial_title VARCHAR(100) NOT NULL,
". "tutorial_author VARCHAR(40) NOT NULL, ".
"submission_date DATE, ". "PRIMARY KEY ( tutorial_id )); ";
cont…
39
mysql_select_db( 'TUTORIALS' );
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not create table: ' . mysql_error());
}
echo "Table created successfully\n";
mysql_close($conn);
?>
</body></html>
cont...
40
if(! $retval )
{
die('Could not delete table: ' . mysql_error());
}
echo "Table deleted successfully\n";
mysql_close($conn);
?>
</body></html>
Lab Example To Connect And Create A Database.
NB: since you don't know the username, servername
and password, make these parameters empty.
44
<?php
$conn = mysql_connect("","" ,"" );
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$sql = 'CREATE Database test_db';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not create database: ' . mysql_error());
}
echo "Database test_db created successfully\n";
mysql_close($conn);
?>