0% found this document useful (0 votes)
0 views

CHapter 6 PHP working with MySQL Database

The document provides a comprehensive guide on how to work with MySQL databases using PHP, covering essential functions such as connecting to a database, creating and selecting databases, creating and deleting tables, and performing data manipulation operations like inserting, updating, and deleting records. It includes code examples for each operation to illustrate the usage of PHP functions like mysql_connect, mysql_query, and mysql_fetch_array. Additionally, it suggests an exercise to create a form for user data submission into a database.

Uploaded by

Tesfaye Desalegn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

CHapter 6 PHP working with MySQL Database

The document provides a comprehensive guide on how to work with MySQL databases using PHP, covering essential functions such as connecting to a database, creating and selecting databases, creating and deleting tables, and performing data manipulation operations like inserting, updating, and deleting records. It includes code examples for each operation to illustrate the usage of PHP functions like mysql_connect, mysql_query, and mysql_fetch_array. Additionally, it suggests an exercise to create a form for user data submission into a database.

Uploaded by

Tesfaye Desalegn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CH-6

PHP working with


MySQL Database
With PHP, you can connect to and manipulate databases.
➢ MySQL is the most popular database system used with PHP.
✓ is a database system used on the web
✓ is a database system that runs on a server
✓ is ideal for both small and large applications
✓ is very fast, reliable, and easy to use
✓ It uses standard SQL
✓ PHP combined with MySQL are cross-platform (you can develop in
Windows & serve on a Unix platform) & is free to download &use
✓ MySQL is named after co-founder Monty Widenius's daughter: My

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

Pwd: Optional. Specifies the password to log in with. Default is ""

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

mysql_query() function used to


drop database.
DROP DATABASE databasename;

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;

Inserting Data to MySQL DB


➢Data can be entered into MySQL tables by
executing SQL INSERT statement through
PHP function mysql_query.

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

• Create a form with name and password


• Create a database named ‘Member’
• Create a table named ‘Admin’
• Insert the form data to ‘Admin’ table
when the user clicks submit.

20
End

21

You might also like