{"id":273,"date":"2026-01-23T05:19:36","date_gmt":"2026-01-23T05:19:36","guid":{"rendered":"https:\/\/www.kwwd.co.uk\/blog\/?p=273"},"modified":"2026-01-24T04:53:29","modified_gmt":"2026-01-24T04:53:29","slug":"loop-through-recordset-with-mysql-and-php-mysqli-connection","status":"publish","type":"post","link":"https:\/\/www.kwwd.co.uk\/blog\/loop-through-recordset-with-mysql-and-php-mysqli-connection\/","title":{"rendered":"Loop Through Recordset With MySQL and PHP (MYSQLI Connection)"},"content":{"rendered":"<p>This is the basic syntax required to loop through a MySQL table and output the results<\/p>\n<pre><code class=\"language-php line-numbers\">\r\n&lt;?php\r\n\/\/ Connect to your database\r\n$conn = mysqli_connect(\"localhost\", \"username\", \"password\", \"database_name\", \"Port Number If Needed\");\r\n\r\n\/\/ Check connection\r\nif (!$conn) {\r\n    die(\"Connection failed: \" . mysqli_connect_error());\r\n}\r\n\r\n\/\/ 2. Perform the query\r\n$sql = \"SELECT ID, Field1, Field2 FROM YourTable\";\r\n$result = mysqli_query($conn, $sql);\r\n\r\n\/\/ Loop through Results if we have some\r\nif (mysqli_num_rows($result) &gt; 0) {\r\n    echo \"&lt;ul&gt;\";\r\n    \r\n    \/\/ Fetch each row as an associative array\r\n    while($row = mysqli_fetch_assoc($result)) {\r\n        \/\/ Output the data\r\n        echo \"&lt;li&gt;ID: \" . $row[\"Field1\"] . \" - \" . $row[\"Field2\"] . \"&lt;\/li&gt;\";\r\n    }\r\n    \r\n    echo \"&lt;\/ul&gt;\";\r\n} else {\r\n    echo \"No records found.\";\r\n}\r\n\r\n\/\/ Close the connection (optional, as PHP does this at end of script \r\n\/\/ but it's good practice to tidy up)\r\nmysqli_close($conn);\r\n?&gt;\r\n<\/code><\/pre>\n<p>If you&#8217;re using a variable to pull out specific records then you need to use a &#8220;Bind&#8221; statement. This ensures you can have and SQL injection issues. We&#8217;ve also added some basic checks to ensure we have the ID required for the select statement and that it&#8217;s a valid number:<\/p>\n<pre><code class=\"language-sql line-numbers\">\r\nif (isset($_GET['id'])) \r\n{\r\n    $ID = (int)$_GET['id']; \/\/ Make Sure $ID is a number\r\n\r\n    if($ID&gt;0)\r\n    {\r\n        \/\/ If we have a valid ID number\r\n        $stmt = mysqli_prepare($conn, \"SELECT Field1, Field2 FROM YourTable WHERE ID = ?\");\r\n        mysqli_stmt_bind_param($stmt, \"i\", $ID); \/\/ \"i\" means the ID is an integer \"s\" is string\r\n        mysqli_stmt_execute($stmt);\r\n        $result = mysqli_stmt_get_result($stmt);\r\n        \r\n        \/\/ If we expect multiple resutls, we'd use a while loop here\r\n        while ($row = mysqli_fetch_assoc($result)) \r\n            {\r\n                    echo $row['Field1'] . \"&lt;br&gt;\";\r\n            }\r\n    } \r\n    else \r\n    {\r\n        echo \"Invalid ID provided.\";\r\n    }\r\n} \r\nelse \r\n{\r\n    echo \"No ID specified.\";\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is the basic syntax required to loop through a MySQL table and output the results &lt;?php \/\/ Connect to your database $conn = mysqli_connect(&#8220;localhost&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,4],"tags":[48,31,24,9,69],"class_list":["post-273","post","type-post","status-publish","format-standard","hentry","category-mysql","category-php","tag-data","tag-database","tag-mysql","tag-php","tag-records"],"_links":{"self":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=273"}],"version-history":[{"count":0,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/273\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}