CHapter 6 PHP working with MySQL Database
CHapter 6 PHP working with MySQL Database
1
Opening Database Connection
Before we can access data in the MySQL database,
we need to be able to connect to the server:
➢ mysql_connect function is used to open a
database connection
➢ This function returns TRUE on success, or
FALSE on failure.
➢ have 3 main parameters
➢ mysql_connect(‘host’,’username’,’pwd’);
2
Parameter Description
Host: Optional. Specifies the server to connect to (can also
include a port number, e.g. "hostname:port").
Default value is "localhost:3306“Optional.
Username: Specifies the username to log in with. Default
value is the name of the user that owns the server process
3
<?php
$con = mysql_connect(“localhost”,”root”,”root”);
if (!$con)
{
die(‘error connecting ’ . Mysql_error());
}
//code here
mysql_close();// closes the connection
?>
4
Creating Database in PHP
➢PHP uses mysql_query function to create
a MySQL database.
➢takes two parameters and returns TRUE
on success or FALSE on failure
➢mysql_query( sql, connection );
5
<?php
$con = mysql_connect(“localhost”,”root”,”root”);
if (!$con)
{
die(“error connecting” . Mysql_error());
}
echo “Connection successful”;
$createdb = mysql_query(“CREATE Database dbname”, $con );
if(! $createdb )
{
die(“Could not create database: “. mysql_error());
}
mysql_close();
?>
6
Selecting a Database
➢Once you establish a connection with a
database server then it is required to
select a particular database where your
all the tables are associated
➢b/c there may be multiple DBs on the
server
mysql_select_db (db_name, con);
7
<?php
$con = mysql_connect(“localhost”,”root”,”root”);
if (!$con)
{
die(“error connecting “ . Mysql_error());
}
If(mysql_select_db(“db_name”, $con))
{
echo “database selected”;
}
mysql_close();
?>
8
Creating Database Tables
➢same thing as creating the database
➢using mysql_query() function.
CREATE TABLE table_name
(
column_name data_type,
PRIMARY KEY(column_name)
);
9
<?php
$con = mysql_connect(“localhost”,”root”,”root”);
if (!$con)
{
die(“error conn ecting “ . Mysql_error());
}
mysql_select_db(“name”, $con);
$sql = “CREATE TABLE student (Name varchar(20),
Id INT(5))”;
mysql_query($sql, $con);
mysql_close();
?>
10
Deleting a Database in PHP
11
<?php
$con = mysql_connect(“localhost”,”root”,”root”);
if (!$con)
{
die(“error connecting ”. Mysql_error());
}
$sql = “DROP DATABASE databasename”;
mysql_query( $sql, $con );
mysql_close();
?> 12
Deleting a Tables in PHP
mysql_query() function used to drop
table from database.
➢ DROP TABLE tablename;
13
<?php
$con = mysql_connect(“localhost”,”root”,”root”);
if (!$con)
{
die(“error connecting “ . Mysql_error());
}
mysql_select_db(“db_name”, $con);
$sql = “insert into table_name values(‘value1’, ‘value2’)”;
mysql_query($sql, $con);
mysql_close();
?>
14
Getting Data From MySQL
➢by executing SQL SELECT statement through
PHP function mysql_query.
➢mysql_fetch_array() used to fetch.
15
<?php
$con = mysql_connect(“localhost”,”root”,”root”);
if (!$con)
{
die(“error connecting “ . Mysql_error());
}
mysql_select_db(“db_name”, $con);
$sql = "select * from student";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
echo $row['Name'];
echo ': ' . $row['Id'];
echo '<br>';
}
mysql_close();
?>
16
Updating data in MySQL
➢UPDATE statement through PHP
function
➢mysql_query.
Deleting Data from MySQL
➢SQL DELETE statement through PHP
function
➢mysql_query.
17
<?php
$con = mysql_connect(“localhost”,”root”,”root”);
if (!$con)
{
die(“error connecting” . Mysql_error());
}
mysql_select_db(“db_name”, $con);
$sql = "UPDATE student set Name = ‘value1'";
$result = mysql_query($sql) or die("error updating
row" . mysql_error());
mysql_close();
?>
18
<?php
$con = mysql_connect(“localhost”,”root”,”root”);
if (!$con)
{
die(“error connecting “ . Mysql_error());
}
mysql_select_db(“db_name”, $con);
$sql = “DELETE from student WHERE Name = ‘value'";
$result = mysql_query($sql) or die("error Deleting row" .
mysql_error());
mysql_close();
?>
19
Exercise
20
End
21