Php database connectivity
-
Upload
baabtracom-no-1-supplier-of-quality-freshers -
Category
Education
-
view
179 -
download
0
Transcript of Php database connectivity
![Page 1: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/1.jpg)
WELCOME
![Page 2: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/2.jpg)
INTRODUCTION TO PHP-MySQL CONNECTIVITY
![Page 3: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/3.jpg)
What you Benefit ???
By the end of this session you will learn how to use PHP to
● Store the data into database.
● Retrieve data from the backend database in real-time.
![Page 4: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/4.jpg)
TASK OF THE DAYCreate the registration form shown here and store the data passed with it into a database .
![Page 5: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/5.jpg)
INTRODUCTION TO PHP DATABASE CONNECTIVITY
![Page 6: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/6.jpg)
Introduction To PHP Database Connectivity
We had already tried passing data to a server . But..where do we store this data and how…?
![Page 7: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/7.jpg)
How To Connect PHP to Database?
Step 1 : Connecting to MySQL ServerStep 2 : Selecting DatabaseStep 3 : Query Execution
![Page 8: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/8.jpg)
STEP 1 - CONNECTING TO MySQL SERVER
mysql_connect(‘host_name’,’username’,’password’);
Eg: mysql_connect(‘localhost’,’root’,’root’);
![Page 9: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/9.jpg)
STEP 2-SELECTING DATABASE
mysql_select_db(‘Database Name’);
Eg: mysql_select_db(‘baabtra_db’);
![Page 10: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/10.jpg)
STEP 3-QUERY EXECUTION
mysql_query(“query to be executed”);
Eg: mysql_query(“update table baabtra_mentees_tbl set vchr_cmpny_name=’Baabtra’ where pk_int_branch_id=’Caffit’ ”);
![Page 11: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/11.jpg)
Let’s TRY
Why don’t we make it even more interesting by implementing this with our task. So let’s start
![Page 12: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/12.jpg)
TASK
Step1: Create the Registration form
![Page 13: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/13.jpg)
STEP 1Registration.html
![Page 14: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/14.jpg)
Step2
Step 2: Now create a database to store the data passed from registration form
![Page 15: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/15.jpg)
Step 2
Lets create the database create database company_baabtra;
Create a table tbl_baabtra_mentees as shown here
![Page 16: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/16.jpg)
Step 3Step 3: Retrieve the data passed using POST method from register_action.php
Checks if button click is set
File upload
Reading the checked values from
checkbox
![Page 17: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/17.jpg)
Step 4
Step 4: Data is now available at register_action.php. So next step is to store this data into the database.
![Page 18: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/18.jpg)
Step 4
register_action.php
Connects to remote server
![Page 19: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/19.jpg)
Step 4
register_action.phpSelects the database
![Page 20: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/20.jpg)
Step 4
register_action.php Executes the mysql query to store the registered mentee information into database
![Page 21: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/21.jpg)
Step 5
Step 5: Run your registration form from the localhost now
![Page 22: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/22.jpg)
Step 5
![Page 23: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/23.jpg)
Step 6
Step 6: Check your database. It should be updated somewhat like that
![Page 24: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/24.jpg)
Connecting Database to PHP
Now how do we fetch the data stored in database from frontend ??
![Page 25: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/25.jpg)
Retrieving Data From MySQL
There are four different ways to fetch the data from database using PHP.
▪ Mysql_fetch_array()▪ Mysql_fetch_row()▪ Mysql_fetch_assoc()▪ Mysql_fetch_object()
All of the above will return one row from table at a time and then the next row
and so on . So usually we use these functions along with a while loop
![Page 26: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/26.jpg)
mysql_fetch_row()
$query = mysql_query (“select * from tbl_baabtra_mentees");while($fetch=mysql_fetch_row($query)){
echo $fetch[0]; //prints first column the retrieved rowecho $fetch[1]; //prints second column the retrieved row
}
![Page 27: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/27.jpg)
mysql_fetch_assoc()
$query = mysql_query (“select * from tbl_baabtra_mentees");while($fetch=mysql_fetch_assoc($query)){
echo $fetch[‘vchr_uname’]; //prints first column the retrieved rowecho $fetch[‘vchr_phone’]; //prints second column the retrieved row
}
![Page 28: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/28.jpg)
mysql_fetch_array()
$query = mysql_query (“select * from tbl_baabtra_mentees");while($fetch=mysql_fetch_array($query)){
echo $fetch[0]; //prints first column the retrieved rowecho $fetch[‘vchr_phone’]; //prints second column the retrieved row
}
![Page 29: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/29.jpg)
mysql_fetch_object()
$query = mysql_query (“select * from tbl_baabtra_mentees");while($fetch=mysql_fetch_object($query)){
echo $fetch -> ‘vchr_uname’; //prints first column the retrieved rowecho $fetch -> ‘vchr_phone’; //prints second column the retrieved row
}
![Page 30: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/30.jpg)
mysql_fetch_object()
$query = mysql_query (“select * from tbl_baabtra_mentees");while($fetch=mysql_fetch_object($query)){
echo $fetch -> ‘vchr_uname’; //prints first column the retrieved rowecho $fetch -> ‘vchr_phone’; //prints second column the retrieved row
}
“Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead.”
![Page 31: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/31.jpg)
Okay…!! So what are we waiting for now..??
Lets do it then
![Page 32: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/32.jpg)
Let’s Get Back With Our Task
Step 7: Set a header to redirect from ‘register_action.php’ to ‘view_baabtra_mentees.php’
header(‘Location: view_baabtra_mentees.php’);
![Page 33: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/33.jpg)
Let’s Get Back With Our Task
Step 8: Now fetch the data from tbl_baabtra_mentees and display it in a table format.
![Page 34: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/34.jpg)
Step 8
uses mysql_fetch_row function. returns each row of data from the mysql table
![Page 35: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/35.jpg)
Step 8
fetches using the column number
![Page 36: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/36.jpg)
Step 9Step 9: Run the registration form from localhost now
![Page 37: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/37.jpg)
Step 9
On Submit it displays the list of mentees registered
![Page 38: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/38.jpg)
Woooh….!!! That really works…So we are done with our TASK of the day.
That was great…!!!
![Page 39: Php database connectivity](https://reader033.fdocuments.us/reader033/viewer/2022052700/55be9b8cbb61eb9c268b45ba/html5/thumbnails/39.jpg)
END OF THE SESSION