{"id":424,"date":"2021-12-02T08:41:14","date_gmt":"2021-12-02T08:41:14","guid":{"rendered":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/?p=424"},"modified":"2021-12-02T08:41:14","modified_gmt":"2021-12-02T08:41:14","slug":"querying-the-database","status":"publish","type":"post","link":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/2021\/12\/02\/querying-the-database\/","title":{"rendered":"Querying the Database"},"content":{"rendered":"\n<p>Query is sent to MySQL asking to see all the records in the classics table, like this:<\/p>\n\n\n\n<p><strong>$query = &#8220;SELECT * FROM classics&#8221;;<br>$result = $conn->query($query);<\/strong><\/p>\n\n\n\n<p>After that, $rows is set to a value representing the number of rows in the table:<\/p>\n\n\n\n<p><strong>$rows = $result->num_rows;<\/strong><\/p>\n\n\n\n<p>Using the value in $rows, a for loop is then entered to display the contents of each row. Then the program populates the array $row with a row of results by calling the fetch_array method of $result, passing it the constant value MYSQLI_NUM, which  forces the return of a numeric (rather than associative) array, like this:<\/p>\n\n\n\n<p>$row = $result->fetch_array(MYSQLI_NUM);<\/p>\n\n\n\n<p>With the data in $row, it\u2019s now a simple matter to display it within the heredoc echo statement that follows, in which I have chosen to use a &lt;pre> tag to line up the display of each record in a pleasing manner.<br>After the display of each record, there is a second form that also posts to sqltest.php (the program itself) but this time contains two hidden fields: delete and isbn. The delete field is set to yes and isbn to the value held in $row[4], which contains the ISBN for the record.<br>Then a submit button with the name DELETE RECORD is displayed, and the form is closed. A curly brace then completes the for loop, which will continue until all records have been displayed, at which time the $result and $conn object\u2019s close methods are closed to release resources back to PHP:<\/p>\n\n\n\n<p><strong>$result->close();<br>$conn->close();<\/strong><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Query is sent to MySQL asking to see all the records in the classics table, like this: $query = &#8220;SELECT * FROM classics&#8221;;$result = $conn->query($query); After that, $rows is set to a value representing the number of rows in the table: $rows = $result->num_rows; Using the value in $rows, a for loop is then entered &hellip; <a href=\"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/2021\/12\/02\/querying-the-database\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Querying the Database<\/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\/424"}],"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=424"}],"version-history":[{"count":1,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/424\/revisions"}],"predecessor-version":[{"id":425,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/424\/revisions\/425"}],"wp:attachment":[{"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=424"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}