{"id":2433,"date":"2016-04-28T10:40:14","date_gmt":"2016-04-28T17:40:14","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2433"},"modified":"2016-04-28T10:40:14","modified_gmt":"2016-04-28T17:40:14","slug":"getting-one-row-from-a-mysql-query","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2433","title":{"rendered":"Getting one row from a MySQL query"},"content":{"rendered":"<p>Often you just need one value or one row of values from a table. If your query returns a unique row because of the query or because you limited it to one row, then you don\u2019t need to go through the foreach loop to get the values. You can change the fetchAll() to fetch() and use just one line to get the value.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n\/\/ get the current category name\n$qry&nbsp;&nbsp;= &quot;SELECT name FROM `website`.`book_category` &quot;;\n$qry .= &quot;WHERE id = :categoryID&quot;;\n\n$stmt = $dbWG-&gt;prepare($qry);\n$stmt-&gt;bindParam(&#039;:categoryID&#039;, $categoryID);\n$stmt-&gt;execute();\n\n$results = $stmt-&gt;fetch();\n$categoryName = $results[&#039;name&#039;];<\/code><\/pre> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often you just need one value or one row of values from a table. If your query returns a unique row because of the query or because you limited it to one row, then you don\u2019t need to go through the foreach loop to get the values. You can change the fetchAll() to fetch() and &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2433\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Getting one row from a MySQL query<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-2433","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2433"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2433\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}