fetch

Fetch Data from Mysql in PHP using PDO - PHP Data Object



Sharing buttons:

hello friends in previous video I have

show you how to insert data into my SQL

table using PHP data object now in this

video I will show you how to fetch data

from my SQL table and PHP by using this

PHP data object modal

friends this is my simple database name

testing and in this 1/2 table with name

table employee with four fields like

head name gender and designation

I have already inserted some data into

this table I will fetch this data from

this table to web page using PHP data

model friends first I have to make

database connection for this first I

have to define hostname username

password and database name for this I

have write host is equal to locale lost

username is equal to root password is

equal to blank and database is equal to

testing

friends we have used PHP data objects so

first I have write try and catch block

in this if there is any error in try

block code then we can easily identify

and catch block

first I have write code in triblock in

this I have write my SQL database

connection code first

I have write variable connect is equal

to new PHP data object with open and

close bracket and between bracket their

first parameter like I have write

database driver name MySQL with host is

equal to server name which I have

defined above after this I have write

database name is equal to variable

database in which I have defined

database name second parameter is

username and last parameter is password

both I have already define above

after this now I want to set the PHP

data object terror to exception for this

I have right connections straying dollar

connect with set attribute function in

this function there is two parameter

first is PHP data object with two x

double colon attribute error mode and

second is PHP data object two-time :

narrower mode exception

friends suppose there is any error and

database connection or insert data so we

can easily handle by PHP data object

exception for I have defined PHP data

object exception at catch by writing

open and close bracket and between

bracket I have write PHP data object

exception with defined one object of

this exception like dollar error

so friends if there is any database

error occur so we can simply get error

message by using this PHP data object

exception object dollar error but now I

want to print her message for I have go

catch block and simply write echo

statement and use PHP data object

exception object which is dollar error

so simply write dollar error with get

message function with help of this

function we get specific error message

you

now friends you can show database

connections successfully done now I have

shown you how to select data from table

by using PHP data object first I have

shown you select all data from table for

fetch all data from table so I have

write query variable in this variable I

will store simple SQL query like select

star from table employee with the help

of this query we can fetch all data from

table

now friends I want to execute this query

and store data into one day to variable

so for this I have write data variable

is equal to database connections string

with object operator with query function

with parameter s ql query which i have

store into query variable this query

function executes an SQL statement

returning a result set as a PDA plant

object

after execute query first I want to

print table with attribute with is equal

to 70% border is equal to 1 cell padding

is equal to 5 cell spacing is equal to 5

in this table I have write 4 column with

name it name gender and designation

I have used for each loop with help of

this loop we can access all the fields

of associative array so I have right for

each with open and closed bracket and

between bracket I have right data

variable as dollar row variable with

help of foreach loop we can access

dollar data variable fields with

reference of row variable

in this loop I want print data and table

format for this I have right taco

statement with open and close table row

tag and between this tag I have write

for table data tag and in first table

data table I have right row with it

column name in second table data tag I

have right row variable with field name

in third table data tag I have right row

variable with field name gender and in

for table data tag I have right row with

table column name designation

and lastly I want to clothes table tag

for this I have right taco statement

with clothes table tag now friends save

this code and see output in browser

friends you can show we can fetch all

data from table to this web page by

using PHP data object now suppose you

want to select single records by using

PHP data object model so now I have show

you how select single data from table

using PHP data object

friends for selecting single records I

have used prepare statement of PHP data

object so first I have write statement

variable is equal to database connection

string which is dollar connect with

object operator with prepare statement

with open and close bracket and between

bracket I have write query like select

start from table employee where it is

equal to double colon IDI here I have

used name placeholder and prepare

statement

now friends I want to execute this

prepare statement query for this I have

rights statement variable with object

operator with the execute function with

open and closed bracket and between

bracket I have write it as equal to one

an array format like this this execute

function executes the prepared statement

now I want to display data and table

formats so I have simply copy above code

here and remove data variable and write

dollar statement variable in which we

have store select data resolved from

preached loop I have saved this code

uncheck output in browser

friends you can see only one data is

display with it was equal to 1 suppose

friends you want to get data if it was

equal to 5 so I have simply go to PHP

coding part and change an array with it

from 1 to 5 and execute function and

save this code uncheck output in browser

friends you can see employee data with

it is equal to 5 so this way you can

fetch data from my SQL table by using

the one PHP so this is my video on how

to select all or select single data from

table and display on web page in PHP by

using PHP data object modal so friends

thank you for watching this video if you

have any query regarding this video you

can comment on this video I will reply

on your comment friends you want to get

more update of my video so friends you

can subscribe my youtube channel friends

you can also like my Facebook page also

or you can also follow webs list