{"id":400,"date":"2021-11-29T11:48:29","date_gmt":"2021-11-29T11:48:29","guid":{"rendered":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/?p=400"},"modified":"2021-11-29T13:09:28","modified_gmt":"2021-11-29T13:09:28","slug":"fetching-a-row","status":"publish","type":"post","link":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/2021\/11\/29\/fetching-a-row\/","title":{"rendered":"Fetching a row"},"content":{"rendered":"\n<p>Fetching results one row at a time, <a href=\"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/data\/fetching_row.txt\">code here<\/a><\/p>\n\n\n\n<p>In this modified code, only one-fifth of the interrogations of the $result object are made (compared to the previous example), and only one seek into the object is made in each iteration of the loop, because each row is fetched in its entirety via the fetch_array method. This returns a single row of data as an array, which is then assigned to the array $row.<\/p>\n\n\n\n<p>The fetch_array method can return three types of array according to the value passed to it:<\/p>\n\n\n\n<p><strong>MYSQLI_NUM<\/strong><br>Numeric array. Each column appears in the array in the order in which you defined it when you created (or altered) the table. In our case, the zeroth element of the array contains the author column, element 1 contains the title column, and so on.<\/p>\n\n\n\n<p><strong>MYSQLI_ASSOC<\/strong><br>Associative array. Each key is the name of a column. Because items of data are referenced by column name (rather than index number), use this option where possible in your code to make debugging easier and help other programmers better manage your code.<\/p>\n\n\n\n<p><strong>MYSQLI_BOTH<\/strong><br>Associative and numeric array.<br>Associative arrays are usually more useful than numeric ones because you can refer to each column by name, such as $row[&#8216;author&#8217;], instead of trying to remember where it is in the column order. This script uses an associative array, leading us to pass MYSQLI_ASSOC.<\/p>\n\n\n\n<p><strong>Closing a connection<\/strong><\/p>\n\n\n\n<p>PHP will eventually return the memory it has allocated for objects after you have finished with the script, so in small scripts, you don\u2019t usually need to worry about releasing memory yourself. However, if you\u2019re allocating a lot of result objects or fetching large amounts of data, it can be a good idea to free the memory you have been using to prevent problems later in your script.<br>This becomes particularly important on higher-traffic pages, because the amount of memory consumed in a session can rapidly grow. Therefore, note the calls to the close methods of the objects $result and $conn in the preceding scripts, as soon as each object is no longer needed, like this:<br>$result->close();<br>$conn->close();<\/p>\n\n\n\n<p>Ideally, you should close each result object when you have finished<br>using it, and then close the connection object when your script will<br>not be accessing MySQL anymore. This best practice ensures that<br>resources are returned to the system as quickly as possible to keep<br>MySQL running optimally, and alleviates doubt over whether PHP<br>will return unused memory in time for when you next need it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fetching results one row at a time, code here In this modified code, only one-fifth of the interrogations of the $result object are made (compared to the previous example), and only one seek into the object is made in each iteration of the loop, because each row is fetched in its entirety via the fetch_array &hellip; <a href=\"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/2021\/11\/29\/fetching-a-row\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Fetching a row<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/400"}],"collection":[{"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=400"}],"version-history":[{"count":3,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/400\/revisions"}],"predecessor-version":[{"id":406,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/400\/revisions\/406"}],"wp:attachment":[{"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=400"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}