+ All Categories
Home > Documents > Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP...

Lecture 25 Retrieving data from MySQL using PHP · Outline •Retrieving data from MySQL using PHP...

Date post: 10-Sep-2018
Category:
Upload: voanh
View: 224 times
Download: 0 times
Share this document with a friend
28
Retrieving data from MySQL using PHP Mr. Mubashir Ali Lecturer (Dept. of Computer Science) [email protected] 1 Lecture 25
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

Retrieving data from MySQL using PHP

Mr. Mubashir AliLecturer (Dept. of Computer Science)

[email protected]

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

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

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

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

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

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

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

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

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

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> &nbsp;</td><td> &nbsp;</td><td> &nbsp;</td><td> &nbsp;</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

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

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

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

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

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

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

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

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

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

2.1 CONNECTIONS: User login form

Mubashir Ali - Lecturer (Department of Computer Science).

20

email

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

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

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

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

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

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

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

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

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


Recommended