{"id":34541,"date":"2023-03-22T15:05:33","date_gmt":"2023-03-22T09:35:33","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/"},"modified":"2023-03-22T15:05:33","modified_gmt":"2023-03-22T09:35:33","slug":"solved-single-result-from-database-using-mysqli","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/","title":{"rendered":"[Solved] Single result from database using mysqli"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-14625134\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"14625134\" data-parentid=\"14624509\" data-score=\"79\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>When just a single result is needed, then no loop should be used. Just fetch the row right away.<\/p>\n<ul>\n<li>\n<p>In case you need to fetch the entire row into associative array:<\/p>\n<pre><code>  $row = $result-&gt;fetch_assoc();\n<\/code><\/pre>\n<\/li>\n<li>\n<p>in case you need just a single value<\/p>\n<pre><code>  $row = $result-&gt;fetch_row();\n  $value = $row[0] ?? false;\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>The last example will return the first column from the first returned row, or <code>false<\/code> if no row was returned. It can be also shortened to a single line,<\/p>\n<pre><code>$value = $result-&gt;fetch_row()[0] ?? false;\n<\/code><\/pre>\n<p>Below are complete examples for different use cases<\/p>\n<h2>Variables to be used in the query<\/h2>\n<p>When variables are to be used in the query, then a <strong>prepared statement<\/strong> must be used. For example, given we have a variable <code>$id<\/code>:<\/p>\n<pre><code>$query = \"SELECT ssfullname, ssemail FROM userss WHERE id=?\";\n$stmt = $conn-&gt;prepare($query);\n$stmt-&gt;bind_param(\"s\", $id);\n$stmt-&gt;execute();\n$result = $stmt-&gt;get_result();\n$row = $result-&gt;fetch_assoc();\n\n\/\/ in case you need just a single value\n$query = \"SELECT count(*) FROM userss WHERE id=?\";\n$stmt = $conn-&gt;prepare($query);\n$stmt-&gt;bind_param(\"s\", $id);\n$stmt-&gt;execute();\n$result = $stmt-&gt;get_result();\n$value = $result-&gt;fetch_row()[0] ?? false;\n<\/code><\/pre>\n<p>The detailed explanation of the above process can be found in my <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/phpdelusions.net\/mysqli_examples\/prepared_select\">article<\/a>. As to why you must follow it is explained in this famous question<\/p>\n<h2>No variables in the query<\/h2>\n<p>In your case, where no variables to be used in the query, you can use the <code>query()<\/code> method:<\/p>\n<pre><code>$query = \"SELECT ssfullname, ssemail FROM userss ORDER BY ssid\";\n$result = $conn-&gt;query($query);\n\/\/ in case you need an array\n$row = $result-&gt;fetch_assoc();\n\/\/ OR in case you need just a single value\n$value = $result-&gt;fetch_row()[0] ?? false;\n<\/code><\/pre>\n<p>By the way, although using raw API while learning is okay, consider using some database abstraction library or at least a helper function in the future:<\/p>\n<pre><code>\/\/ using a helper function\n$sql = \"SELECT email FROM users WHERE id=?\";\n$value = prepared_select($conn, $sql, [$id])-&gt;fetch_row[0] ?? false;\n\n\/\/ using a database helper class\n$email = $db-&gt;getCol(\"SELECT email FROM users WHERE id=?\", [$id]);\n<\/code><\/pre>\n<p>As you can see, although a helper function can reduce the amount of code, a class&#8217; method could encapsulate all the repetitive code inside, making you to write only meaningful parts &#8211; the query, the input parameters and the desired result format (in the form of the method&#8217;s name).<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Single result from database using mysqli <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] When just a single result is needed, then no loop should be used. Just fetch the row right away. In case you need to fetch the entire row into associative array: $row = $result-&gt;fetch_assoc(); in case you need just a single value $row = $result-&gt;fetch_row(); $value = $row[0] ?? false; The last example will &#8230; <a title=\"[Solved] Single result from database using mysqli\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/\" aria-label=\"More on [Solved] Single result from database using mysqli\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[391,1117,339],"class_list":["post-34541","post","type-post","status-publish","format-standard","hentry","category-solved","tag-loops","tag-mysqli","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Single result from database using mysqli - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Single result from database using mysqli - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] When just a single result is needed, then no loop should be used. Just fetch the row right away. In case you need to fetch the entire row into associative array: $row = $result-&gt;fetch_assoc(); in case you need just a single value $row = $result-&gt;fetch_row(); $value = $row[0] ?? false; The last example will ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-22T09:35:33+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Single result from database using mysqli\",\"datePublished\":\"2023-03-22T09:35:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/\"},\"wordCount\":245,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"loops\",\"mysqli\",\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/\",\"name\":\"[Solved] Single result from database using mysqli - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-03-22T09:35:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Single result from database using mysqli\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Single result from database using mysqli - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Single result from database using mysqli - JassWeb","og_description":"[ad_1] When just a single result is needed, then no loop should be used. Just fetch the row right away. In case you need to fetch the entire row into associative array: $row = $result-&gt;fetch_assoc(); in case you need just a single value $row = $result-&gt;fetch_row(); $value = $row[0] ?? false; The last example will ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/","og_site_name":"JassWeb","article_published_time":"2023-03-22T09:35:33+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Single result from database using mysqli","datePublished":"2023-03-22T09:35:33+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/"},"wordCount":245,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["loops","mysqli","php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/","url":"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/","name":"[Solved] Single result from database using mysqli - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-03-22T09:35:33+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-single-result-from-database-using-mysqli\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Single result from database using mysqli"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/34541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=34541"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/34541\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=34541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=34541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=34541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}