lecture 25 retrieving data from mysql using php · outline •retrieving data from mysql using php...
TRANSCRIPT
![Page 1: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/1.jpg)
Retrieving data from MySQL using PHP
Mr. Mubashir AliLecturer (Dept. of Computer Science)
1
Lecture 25
![Page 2: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/2.jpg)
Summary of the previous lecture
• FILES super global variable
• File uploading in PHP
• Storing reference of uploaded file in database
• CONNECTIONS: user registration with file upload
Mubashir Ali - Lecturer (Department of Computer Science).
2
![Page 3: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/3.jpg)
Outline
• Retrieving data from MySQL using PHP
• CONNECTIONS: login functionality
Mubashir Ali - Lecturer (Department of Computer Science).
3
![Page 4: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/4.jpg)
1. Retrieving data from MySQL using PHP
• Connection with database
• Execute Select SQL command
• Make display structure
• Write data
Mubashir Ali - Lecturer (Department of Computer Science).
4
![Page 5: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/5.jpg)
1.1 Connection with database
Mubashir Ali - Lecturer (Department of Computer Science).
5
<?php
mysql_connect(“localhost”,”root”,””) or die(“Error in connection”);
mysql_select_db(“testdatabase”) or die(“Error in Selection”);
?>
![Page 6: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/6.jpg)
1.2 Selecting data
Mubashir Ali - Lecturer (Department of Computer Science).
6
• SELECT command in SQL:
SELECT column-name
FROM table-name
SELECT user_Name
FROM users
SELECT *
FROM users
![Page 7: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/7.jpg)
1.2 Selecting data…
Mubashir Ali - Lecturer (Department of Computer Science).
7
• Condition selection:
SELECT column-name
FROM table-name
WHERE condition
SELECT *
FROM users
WHERE user_Id>4
![Page 8: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/8.jpg)
1.2 Selecting data…
Mubashir Ali - Lecturer (Department of Computer Science).
8
<?php
include(‘connection.php’);
$sql = ‘select * from users’;
$result = mysql_query($sql);
?>
![Page 9: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/9.jpg)
1.2 Selecting data…
Mubashir Ali - Lecturer (Department of Computer Science).
9
• Counting rows:– mysql_num_rows(variable);
<?php
include(‘connection.php’);
$sql = ‘select * from users’;
$result = mysql_query($sql);
$users = mysql_num_rows($result);
echo “There are total ”. $users .”users found”;
?>
![Page 10: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/10.jpg)
1.3 Display structure
Mubashir Ali - Lecturer (Department of Computer Science).
10
<table border=‘1’><tr><th> User Name</th><th> User Email</th><th> User Password</th><th> User Picture</th></tr><tr><td> </td><td> </td><td> </td><td> </td></tr></table>
![Page 11: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/11.jpg)
1.4 Writing data
Mubashir Ali - Lecturer (Department of Computer Science).
11
• mysql_fetch_array(result-resource);
– mysql_fetch_array($result);
![Page 12: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/12.jpg)
1.4 Writing data…
Mubashir Ali - Lecturer (Department of Computer Science).
12
$result=
$row = mysql_fetch_array($result);
1 Ali [email protected] 123 upload/123ali.jpg
2 Umar [email protected] 123 upload/123umar.jpg
$row= 1 Ali [email protected] 123 upload/123ali.jpg
user_Id user_Name user_Email user_Password user_Picture
0 41 2 3
echo $row [1]; echo $row[‘user_Name’];
![Page 13: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/13.jpg)
1.4 Writing data…
Mubashir Ali - Lecturer (Department of Computer Science).
13
<table border=‘1’><tr><th> User Name</th><th> User Email</th><th> User Password</th><th> User Picture</th></tr><tr><td> <?php echo $row[1]; ?> </td><td> <?php echo $row[2]; ?> </td><td> <?php echo $row[3]; ?> </td><td> <img src= “<?php echo $row[4]; ?>”> </td></tr></table>
User Name User Email User Password User Picture
Ali [email protected] 123
![Page 14: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/14.jpg)
1.4 Writing data…
Mubashir Ali - Lecturer (Department of Computer Science).
14
<table border=‘1’>Heading Row<?phpwhile($rows = mysql_fetch_array($result)){?><tr><td> <?php echo $row[1]; ?> </td><td> <?php echo $row[2]; ?> </td><td> <?php echo $row[3]; ?> </td><td> <img src= “<?php echo $row[3]; ?>”> </td></tr><?php } ?></table>
User Name User Email User Password User Picture
Ali [email protected] 123
Umar [email protected]
123
![Page 15: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/15.jpg)
1.5 Example
Mubashir Ali - Lecturer (Department of Computer Science).
15
Starts a HTML page
Connection to database
Select command
Query executed
Counting number of rows
![Page 16: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/16.jpg)
1.5 Example…
Mubashir Ali - Lecturer (Department of Computer Science).
16
Heading row
Loop starts Keeps row
![Page 17: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/17.jpg)
1.5 Example…
Mubashir Ali - Lecturer (Department of Computer Science).
17
Displays name
Displays email
Displays password
Displays imageSets source
Ends loop
Ends table
![Page 18: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/18.jpg)
1.5 Example…
Mubashir Ali - Lecturer (Department of Computer Science).
18
Records in user’s table
Output from the table
![Page 19: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/19.jpg)
2. CONNECTIONS: User login
• Form for user’s input
• Login action page:– Connection with database
– Retrieve user’s input
– Select a record from user’s table with same email and password
– Count the number of row in result
– If one row is selected then fetch its values and storein session variable, otherwise send an error message on main page
Mubashir Ali - Lecturer (Department of Computer Science).
19
![Page 20: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/20.jpg)
2.1 CONNECTIONS: User login form
Mubashir Ali - Lecturer (Department of Computer Science).
20
Password
Post method
![Page 21: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/21.jpg)
2.2 CONNECTIONS: database connection
Mubashir Ali - Lecturer (Department of Computer Science).
21
<?php
mysql_connect(“localhost”,”root”,””) or die(“Error in connection”);
mysql_select_db(“testdatabase”) or die(“Error in Selection”);
?>
![Page 22: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/22.jpg)
2.3 CONNECTIONS: Retrieve user’s input
Mubashir Ali - Lecturer (Department of Computer Science).
22
![Page 23: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/23.jpg)
2.4 CONNECTIONS: Select record
Mubashir Ali - Lecturer (Department of Computer Science).
23
![Page 24: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/24.jpg)
2.5 CONNECTIONS: Redirect
Mubashir Ali - Lecturer (Department of Computer Science).
24
No. of rows selected
Fetch user information
Register session variables
redirect
If user’s input is invalid
![Page 25: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/25.jpg)
2.6 CONNECTIONS: user page
Mubashir Ali - Lecturer (Department of Computer Science).
25
User profile
User’s pic
User’s information
actions
![Page 26: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/26.jpg)
2.6 CONNECTIONS: user page…
Mubashir Ali - Lecturer (Department of Computer Science).
26
Profile div
Image div
User’s info
‘picture’
![Page 27: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/27.jpg)
Summary
• Retrieving data from MySQL using PHP
• CONNECTIONS: login page
Mubashir Ali - Lecturer (Department of Computer Science).
27
![Page 28: Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP •CONNECTIONS: login functionality Mubashir Ali - Lecturer (Department of Computer](https://reader030.vdocuments.mx/reader030/viewer/2022020316/5b956b5f09d3f2214e8cc162/html5/thumbnails/28.jpg)
References
• Chapter 30, “Beginning PHP and MySQL” by W. Jason Gilmore, Apress publisher, 4th edition; 2010, ISBN-13 (electronic): 978-1-4302-3115-8.
Mubashir Ali - Lecturer (Department of Computer Science).
28