{"id":5748,"date":"2019-02-04T06:22:47","date_gmt":"2019-02-04T06:22:47","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=5748"},"modified":"2022-04-13T15:56:38","modified_gmt":"2022-04-13T15:56:38","slug":"how-to-use-pdo-to-read-data-from-the-database","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-use-pdo-to-read-data-from-the-database\/","title":{"rendered":"How to use PDO to read data from the database?"},"content":{"rendered":"<p><strong>1)<\/strong> Write the regular select statement and again, instead of values, put named placeholders. For example:<br \/>\n[code language=&#8221;php&#8221;]<br \/>\n$sql = &quot;SELECT * FROM users&quot;;<br \/>\n[\/code]<br \/>\n2) Prepare the query:<br \/>\n[code language=&#8221;php&#8221;]<br \/>\n$query = $dbh -&gt; prepare($sql);<br \/>\n[\/code]<br \/>\n3) Execute the query:<br \/>\n[code language=&#8221;php&#8221;]<br \/>\n$query -&gt; execute();<br \/>\n[\/code]<br \/>\n<strong>4)<\/strong> Assign the data which you pulled from the database (in the preceding step) to a variable.<br \/>\n[code language=&#8221;php&#8221;]<br \/>\n$results = $query -&gt; fetchAll(PDO::FETCH_OBJ);<br \/>\n[\/code]<br \/>\nHere I used the parameter PDO::FETCH_OBJ that returns the fetched data as an object. If you\u2019d like to fetch the data in the form of an array, use: <strong>PDO::FETCH_ASSOC<\/strong>.<br \/>\n<strong>5)<\/strong> Make sure that you were able to retrieve the data from the database, by counting the number of records.<br \/>\n[code language=&#8221;php&#8221;]<br \/>\nif($query -&gt; rowCount() &gt; 0){}<br \/>\n[\/code]<br \/>\n<strong>6)<\/strong> In case that the query returned at least one record, we can echo the records within a foreach loop:<br \/>\n[code language=&#8221;php&#8221;]<br \/>\nif($query -&gt; rowCount() &gt; 0)<br \/>\n{<br \/>\nforeach($results as $result)<br \/>\n{<br \/>\necho $result -&gt; name . &quot;, &quot;;<br \/>\necho $result -&gt; city . &quot;, &quot;;<br \/>\necho $result -&gt; date_added;<br \/>\n}<br \/>\n}<br \/>\n[\/code]<br \/>\n<strong>All code together now:<\/strong><br \/>\n[code language=&#8221;php&#8221;]<br \/>\n$sql = &quot;SELECT * FROM users WHERE city = :city&quot;;<br \/>\n$query = $dbh -&gt; prepare($sql);<br \/>\n$query -&gt; bindParam(&#8216;:city&#8217;, $city, PDO::PARAM_STR);<br \/>\n$city = &quot;New York&quot;;<br \/>\n$query -&gt; execute();<br \/>\n$results = $query -&gt; fetchAll(PDO::FETCH_OBJ);<br \/>\nif($query -&gt; rowCount() &gt; 0)<br \/>\n{<br \/>\nforeach($results as $result)<br \/>\n{<br \/>\necho $result -&gt; name . &quot;, &quot;;<br \/>\necho $result -&gt; city . &quot;, &quot;;<br \/>\necho $result -&gt; date_added;<br \/>\n}<br \/>\n}<br \/>\n[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1) Write the regular select statement and again, instead of values, put named placeholders. For example: [code language=&#8221;php&#8221;] $sql = &quot;SELECT * FROM users&quot;; [\/code] 2) Prepare the query: [code&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5150],"tags":[5179,5176,5173,5178],"class_list":["post-5748","post","type-post","status-publish","format-standard","hentry","category-php","tag-how-fetch-data-from-database-using-pdo","tag-pdo","tag-php-data-object","tag-php-pdo"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5748","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=5748"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5748\/revisions"}],"predecessor-version":[{"id":5751,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5748\/revisions\/5751"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=5748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=5748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=5748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}