How to use PDO to read data from the database?

Reading data from the database is not so different than inserting data, with steps 1 to 5 being almost identical while the sixth step is different.

1) Write the regular select statement and again, instead of values, put named placeholders. For example:

2) Prepare the query:
3) Execute the query:
4) Assign the data which you pulled from the database (in the preceding step) to a variable.
Here I used the parameter PDO::FETCH_OBJ that returns the fetched data as an object. If you’d like to fetch the data in the form of an array, use: PDO::FETCH_ASSOC.
5) Make sure that you were able to retrieve the data from the database, by counting the number of records.
6) In case that the query returned at least one record, we can echo the records within a foreach loop:
All code together now:
For localhost Database name is : pdoanuj
Sql file available inside the package.
[aio_button align=”none” animation=”none” color=”red” size=”small” icon=”play” text=”Demo” target=”_blank” relationship=”dofollow” url=””]        [aio_button align=”none” animation=”none” color=”red” size=”small” icon=”download” text=”Download” relationship=”dofollow” url=””]

Anuj Kumar

I'm Anuj kumar a web developer with 4+ year experience .I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to is provide php , jquery , mysql , phpoops and other web development tutorials. I am trying best effort to make PHPGurukul useful for every single moment spend on this website. If you think this website is useful to visit please share with your friend and buddies.

Recommended Tutorials for you

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *