Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
WELCOME
INTRODUCTION TO
PHP-MySQL CONNECTIVITY
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.
TASK OF THE DAY
Create the registration form shown here and store the data passed with it into a
database .
INTRODUCTION TO PHP DATABASE
CONNECTIVITY
Introduction To PHP Database
Connectivity
We had already tried passing data to a server .
But..where do we store this data and how…?
How To Connect PHP to Database?
Step 1 : Connecting to MySQL Server
Step 2 : Selecting Database
Step 3 : Query Execution
STEP 1 -
CONNECTING TO MySQL SERVER
mysql_connect(‘host_name’,’username’,’password’);
Eg: mysql_connect(‘localhost’,’root’,’root’);
STEP 2-
SELECTING DATABASE
mysql_select_db(‘Database Name’);
Eg: mysql_select_db(‘baabtra_db’);
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’ ”);
Let’s TRY
Why don’t we make it even more interesting by implementing this with our task.
So let’s start
TASK
Step1: Create the Registration form
STEP 1
Registration.html
Step2
Step 2: Now create a database to store the data passed from registration form
Step 2
Lets create the database
create database company_baabtra;
Create a table tbl_baabtra_mentees as shown here
Step 3
Step 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
Step 4
Step 4: Data is now available at register_action.php. So next step is to store this
data into the database.
Step 4
register_action.php
Connects to remote server
Step 4
register_action.php
Selects the database
Step 4
register_action.php Executes the mysql query to
store the registered mentee
information into database
Step 5
Step 5: Run your registration form from the localhost now
Step 5
Step 6
Step 6: Check your database. It should be updated somewhat like that
Connecting Database to PHP
Now how do we fetch the data stored in database from frontend ??
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
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 row
echo $fetch[1]; //prints second column the retrieved row
}
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 row
echo $fetch[‘vchr_phone’]; //prints second column the retrieved row
}
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 row
echo $fetch[‘vchr_phone’]; //prints second column the retrieved row
}
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 row
echo $fetch -> ‘vchr_phone’; //prints second column the retrieved row
}
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 row
echo $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.”
Okay…!!
So what are we waiting for now..??
Lets do it then
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’);
Let’s Get Back With Our Task
Step 8: Now fetch the data from tbl_baabtra_mentees and display it in a
table format.
Step 8
uses mysql_fetch_row function.
returns each row of data from the
mysql table
Step 8
fetches using the column
number
Step 9
Step 9: Run the registration form from localhost now
Step 9
On Submit it displays the list of mentees registered
Woooh….!!! That really works…
So we are done with our TASK of the day.
That was great…!!!
END OF THE SESSION

More Related Content

Php database connectivity

  • 3. 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.
  • 4. TASK OF THE DAY Create the registration form shown here and store the data passed with it into a database .
  • 5. INTRODUCTION TO PHP DATABASE CONNECTIVITY
  • 6. Introduction To PHP Database Connectivity We had already tried passing data to a server . But..where do we store this data and how…?
  • 7. How To Connect PHP to Database? Step 1 : Connecting to MySQL Server Step 2 : Selecting Database Step 3 : Query Execution
  • 8. STEP 1 - CONNECTING TO MySQL SERVER mysql_connect(‘host_name’,’username’,’password’); Eg: mysql_connect(‘localhost’,’root’,’root’);
  • 9. STEP 2- SELECTING DATABASE mysql_select_db(‘Database Name’); Eg: mysql_select_db(‘baabtra_db’);
  • 10. 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’ ”);
  • 11. Let’s TRY Why don’t we make it even more interesting by implementing this with our task. So let’s start
  • 12. TASK Step1: Create the Registration form
  • 14. Step2 Step 2: Now create a database to store the data passed from registration form
  • 15. Step 2 Lets create the database create database company_baabtra; Create a table tbl_baabtra_mentees as shown here
  • 16. Step 3 Step 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
  • 17. Step 4 Step 4: Data is now available at register_action.php. So next step is to store this data into the database.
  • 20. Step 4 register_action.php Executes the mysql query to store the registered mentee information into database
  • 21. Step 5 Step 5: Run your registration form from the localhost now
  • 23. Step 6 Step 6: Check your database. It should be updated somewhat like that
  • 24. Connecting Database to PHP Now how do we fetch the data stored in database from frontend ??
  • 25. 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
  • 26. 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 row echo $fetch[1]; //prints second column the retrieved row }
  • 27. 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 row echo $fetch[‘vchr_phone’]; //prints second column the retrieved row }
  • 28. 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 row echo $fetch[‘vchr_phone’]; //prints second column the retrieved row }
  • 29. 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 row echo $fetch -> ‘vchr_phone’; //prints second column the retrieved row }
  • 30. 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 row echo $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.”
  • 31. Okay…!! So what are we waiting for now..?? Lets do it then
  • 32. 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’);
  • 33. Let’s Get Back With Our Task Step 8: Now fetch the data from tbl_baabtra_mentees and display it in a table format.
  • 34. Step 8 uses mysql_fetch_row function. returns each row of data from the mysql table
  • 35. Step 8 fetches using the column number
  • 36. Step 9 Step 9: Run the registration form from localhost now
  • 37. Step 9 On Submit it displays the list of mentees registered
  • 38. Woooh….!!! That really works… So we are done with our TASK of the day. That was great…!!!
  • 39. END OF THE SESSION