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

Lecture 7

This document discusses how to connect to and interact with a MySQL database from PHP. It covers connecting to the database, executing SQL statements like SELECT, INSERT, UPDATE and DELETE, and handling exceptions. The document provides code examples for connecting to MySQL, creating and dropping databases, and executing queries from PHP.

Uploaded by

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

Lecture 7

This document discusses how to connect to and interact with a MySQL database from PHP. It covers connecting to the database, executing SQL statements like SELECT, INSERT, UPDATE and DELETE, and handling exceptions. The document provides code examples for connecting to MySQL, creating and dropping databases, and executing queries from PHP.

Uploaded by

sumaya ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Lecture seven

Presented by:
Mohamed Ahmed.
Chapter 4
How to use PHP with aMySQL
database
Objectives
Applied
1. Given the specifications for a database application that requires
only the skills that are presented in this chapter, develop the
application. That includes:
 Connecting to a MySQL database
 Executing SELECT, INSERT, UPDATE, and DELETE
statements
 Handling PDO exceptions
 Getting the data from the result sets that are returned by SQL
statements

Chapter 4
Slide 3
Objectives cont…
Knowledge
1. Describe the PHP code for creating a PDO object that connects to
a MySQL database.
2. Describe the use of the PDO methods for executing a SELECT,
INSERT, UPDATE, or DELETE statement.
3. Describe the PHP code for handling the PDO exceptions that may
occur when you try to create a PDO object.
4. Describe a PHP array and the way that numeric and string indexes
are used to access the data in a PHP array.
5. Describe the way the fetch method of a PDOStatement object is
used to get data from the first row of a result set and the way a
foreach statement is used to get the data from all the rows of a
result set.

Chapter 4
Slide 4
PHP for working with MySQL

Chapter 4 Slide 5
Chapter 4 Slide 6
Terms
class
object
argument
PDO object
DSN (Data Source Name)
PDO (PHP Data objects) extension
method

Chapter 4
Slide 7
How to execute SELECT statements

Chapter 4 Slide 8
Chapter 4 Slide 9
How to execute INSERT, UPDATE, and DELETE statements

Chapter 4 Slide 10
Slide 11
Chapter 4
Chapter 4 Slide 12
How to use a try/catch statement to handle
exceptions

An exception is an object that contains


information about an error that has occurred.

If the exception is not handled, the application


will end.

Chapter 4 Slide 13
Chapter 4 Slide 14
Chapter 4 Slide 15
Terms
exception
exception handling
throw an exception
try/catch statement
try block
catch block
Exception class
PDOException class

Chapter 4
Slide 16
Connection to MySQL Database

<?php
// connection to mysql

$con=mysql_connect("localhost","root","") or die ("could not


connected");

echo "connected successfuly";

mysql_close($con);

?>

Chapter 4
Slide 17
Connection in other way
<?php
// connection to mysql

$con=@mysql_connect("localhost","root","") or die
(mysql_error());

echo " connection is ok";

mysql_close($con);

?>

Chapter 4
18
Connection in other method
<?php
// connection to mysql

$con=@mysql_connect('localhost','root','');

if (!$con){
die ('could not connect:' .mysql_error());
}

echo 'connected successfuly';

mysql_close($con);

?>
Chapter 4
19
Create Database during PHP code
<?php
// connection to mysql
$con=@mysql_connect("localhost","root","");
if (!$con){
die ("could not connect:" .mysql_error());
}
$sql='create database data1';
if (mysql_query($sql,$con)){
echo "Your database is creating successfuly \n";
}
else {
echo "Your database is not creating";
}
?>
Chapter 4
20
Drop or Delete Database during PHP code
<?php
// connection to mysql
$con=@mysql_connect("localhost","root","");
if (!$con){
die ("could not connect:" .mysql_error());
}
$sql='drop database data1';
if (mysql_query($sql,$con)){
echo "Your database is dropping successfuly \n";
}
else {
echo "Your database is not dropping";
}
?>
Chapter 4
21
Create Database in other method during PHP code
<?php
// connection to mysql
$con=@mysql_connect('localhost','root','')
or die (mysql_error());

if (mysql_create_db("data1") ){
echo "Your database is created ";
}
else {
echo "Your database is not created";
}
?>

Chapter 4
22
Drop Database in other method during PHP code
<?php
// connection to mysql
$con=@mysql_connect('localhost','root','')
or die (mysql_error());
if (mysql_drop_db("data1")){
echo "Your database is dropped";
}
else {
echo "Your database is not dropped";
}
?>

Chapter 4
23
Any Question??
Thanks

You might also like