hello guys welcome back to another video
tutorial in this video i'm gonna teach
you how you can fetch the data from
database in php and how you can display
the data inside your table
i made the video on this topic couple of
years ago but this time i'm gonna show
you
two best way to display the data from
database
right
so i already
opened the
apache in mysqli and i'm going to show
you my skill local host
phpmyadmin first i'm going to show you
inside this page
have a tutorials database i have made
and inside the
database server table with the name of
users right i'm going to open this one
table
inside they have more than
four columns one two three four we will
use four columns to get the data from
there
and display the data inside of our
html table right
so
i'm gonna also show you my tables
um i'm gonna click in this
uh i just show you structure of this one
table right
so this is a integer uh user id and this
is the auto increment
uh
value means it will change the
automatically number
with
the help of uh inserting the card when i
will insert the record inside the table
then this number this id will
automatically generate right
so i made the database with the name of
tutorials and the table name is users i
have
made the file inside htdocs
with the name of data inside the data
in a css folder i just designed the
table with the help of css i mean
bootstrap.css
min.css
so this project i have open on my vs
code editor so let's get start to work
on that so i'm going to create an index
dot php file
inside there i'm going to write the
basic structure of html and i'm going to
call fetch data
from database
and php
and i want to run this one project so i
open my first one close this one i use
local host and
my variable name
i mean
folder name right
so
i just
open this one and sell this one refer
the page as you can see title has been
changed now page data from database and
php
so first of all i will include the
link relationship
uh css inside this here have distributed
main.css
so inside this one section i will create
a
i will design simple page
fine and
i'll use class container
okay class
uh
the second day will be
so
and third will be
um
call
all right
inside the call i'm going to create a do
write the class card
right inside the card and creator again
there
write the
class card header all right this is a
simple designing i just designed the
header and body of the card
so inside there i'm going to call h2 and
why the fetch data
from database and php
and assign the
class
displays
six
so sir let's see output of this one
perfect i want to display this one text
inside the center so i call
text will be center
and i will assign the
class inside the body
vg
dot
so let's see output of this one
perfect
and i want to display uh this one area
on below so i will use margin top on the
card
empty file
and also i will use empty file on this
one
to display the data
below
so this is our simple card then we will
create a table right
so this is a card section
and a heater section card and this is a
card
card detail so inside there i'm going to
create a class assign the class card
body
right and inside the card body i will
create a
table so i'm going to call t r naught
here i'm going to call table type first
then tr and table or 3d means table data
and say that i'm going to call user id i
will assign the static data
and this will be first
name
and this will be last name
and last one will be email right
so first let's see output of this one
we need to assign the class
on the table tag
table
table border
so let's see
right
i want to display those text inside the
center so i call text center inside
there
and i
copy and paste second time i just show
you sir
i want to display this one uh
dark so i will use class
b g dog
text white
so let's see
perfect
so inside this one section we will
display the data from database we will
get the data from database and display
the data on there right
so let me explain you how you can do
that
the first thing i will create a folder
i'm going to write the folder name
config inside the config i will create a
db.php file
inside the db uh file i'm gonna
create a connection
all right the connection is very
important
so you can write any name
any variable name right i'm gonna call
con and assign the value mysqli
connect
right inside there you need to pass four
parameters the first one is a local host
your database uh i mean
i'm gonna show you
your php my admin details i'm gonna show
you i go to where i click on this one
and
[Music]
settings
are not settings i think users
here is important thing on there
so this is the username is root
and
host name localhost you need to use uh
i'm going to show you
localhost and username you need to use
this one
so i'm gonna write the second parameter
comma username is root and third
parameter we need to enter the password
so password is no so you need to
use empty field on there and then you
need to use comma and the fourth
parameter you need to add your database
name my database name is tutorials as
you can see this one
so you need to use this one database
tutorials
so inside there i'm gonna use if
statement and call if con is not active
means
if i have a problem inside my connection
then i want to display the error message
connection
so i will open this one file on my
browser
so i call
data then the folder name is config.not
and say that i have a db dot php
as you as you can see
there is no any error appeared on there
so if i will change the database name
like this one
then you will able to see error right
so everything is a perfect on there
so we don't need to change anything else
so call again data
your main page so we need to get the
data from database
so the step number one is very simple
you need to go on index.php
and say that you need to use
basic structure of php
then you need to get the database file
inside this one page because you will
use db file on there
so i'm going to call require once
and not this one i call uh yes i will
call a config folder inside a config
folder db.php
then the next thing you need to uh
write the query
that theory is very simple
create a variable
and assign the value assign the query
detail on this one variable
so the query is select static form
and your table name my table name is
users
i'm going to show you
i go to uh this one this is my database
name and say there are the server table
as you can see
database name table name is users so you
need to use that
and then the next thing you need to
create another variable and use mysql i
query
past two parameter the first one is a
connection and second parameter you need
to get
the query variable
this variable appeared on my this one
file as you can see
after including this one
function on there then you will able to
access
this one variable on index.php right
once you do that then you need to go on
below
inside this one section
you will use while loop i'm going to
show you very easy way i already made a
video on this one topic but this time
i'm going to show you
also another method
so right inside the tr tag as you can
see this is the tr starting tr closing
first let's save that and let's see
output of this one
you can't see the data right
so i will write basic structure of php
right
inside there
i call while loop and call
this is a row variable called mysqli h
associate
inside there you need to pass
this one variable this variable will get
the
this variable will help us to run the
query and get the data inside this one
variable
so you need to paste on there
use curly brackets
i'm going to show you very important
thing on there
this is a starting this is a closing so
you need to close the php take on there
right
and you need to run the php tag
on there
right
means inside this one section this one
i'm gonna show you
this one section
you will write the
tr and td tags
then our while loop is working
to getting the data from database and
display the data on the page every time
it's working every time this is a loop
process
right so i will use tr i already made
the tr on there right as you can see top
of the php and we need to
cut this one and paste on there
uh not there
based on there this one section
right
this is starting this is closing
inside there i use 3d
and inside there i call php starting and
closing tag and call echo
statement dollar row this one variable
and
use
array
and inside the single quotation you need
to
add the database
column name so i'm going to show you
our database column name is user id so
you need to use user underscore id first
i'm going to show you this way say
and
and let's see as you can see you will
get the data
from this one column username user id
one two three four five
so i want to display more than first
name last name and uh email three values
so i just
use second third and fourth
we need to change the column name
the second one is the name third one is
the last name fourth one is email so i'm
gonna use
this one on
name
and this one is last
name and third one is email so first
solve that and let's see
as you can see
we have received the data from database
and display the data inside the table
right
and other thing is i want to display the
column like edit and delete on there
so you need to uh use this section
and i'm going to use
edit
copy and paste i'd user
and this will be deleted first have that
let's see
two columns has have been appeared
inside our table so i want to display
the
details on there so i go to on this one
section i
use
i'm going to show you from skate gd
inside there i'm going to call aha
this is a hash because i don't need to
use any other operations like a delete
data edit data i just show you
so
after the hash sign i'm going to assign
the class
btn
btn primary
inside that i'm going to highlight it
i use same thing and i just change that
to
danger
and inside that i'm going to delete the
so so let's see
as you can see guys
right
so this is a very simple and easy way to
get the data from database and display
the data inside the table the last thing
i'm going to show you
so
first of all i
i want to use everything on another page
inside this one page i just use this
section
because i want to display the data
inside this one page right so i'm going
to show you another way inside the
config.php
i mean config folder i will create a
file like
functions.php all right
so write the basic structure of php
right
so inside there i'm going to call
require once i need to include the
database because
i will use the database on there so i
call
db.php
so i'm going to call function
and
display
data all right use calibrate inside
there
and say the function you need to call
global and column variable because you
will use the cone inside the function
section right
so you need to use this then i'm going
to write the query which query
i used on earlier select static from
users this is my table name
then i call again result variable called
mysqli qd inside there we need to pass
two parameters first one is the con
this one and second one is query
variable right
inside there have a problem as you can
see this is the error has been appeared
so what i need to do on there i just
call return
and result variable
okay we need to use this
right
once you use this function then you will
able to access same data from database
with the help of function
so i copy this one function and go to
index.php i comment i just use comment
and this will be on there right
for something said that you will see
many errors on there as you can see
right
but i will use function
we need to get the data
i call require once and
we need to
call
config
config.db
and also we need to
add another file with the name of
uh
not
config folder inside the config folder
db.php
and inside the config folder here
function.php because we will use
function inside this one page
right
so inside there i'm going to create a
result variable you can write any name
and call actual sign and paste
the
function
name
then this function
will get the data from database and
assign the data inside of result
variable
you can use same variable on
this one section
first cell there let's see output of
this one
as you can see everything is the same
now
so you can use any method as you want i
have explained you all about how you can
affect the data from database i explain
you how you can get the data
from database in single page and also
another page i've used this method to
get the data from
i just fight
bunch of code inside another file and i
just call the
file inside there
so i hope you are understood as well
thank you very much for watching have a
nice day