{"id":383,"date":"2021-11-29T08:40:13","date_gmt":"2021-11-29T08:40:13","guid":{"rendered":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/?p=383"},"modified":"2021-11-29T11:43:27","modified_gmt":"2021-11-29T11:43:27","slug":"connecting-to-a-mysql-database","status":"publish","type":"post","link":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/2021\/11\/29\/connecting-to-a-mysql-database\/","title":{"rendered":"Connecting to a MySQL Database"},"content":{"rendered":"\n<p>Now that you have saved the login.php file, you can include it in any PHP files that will need to access the database by using the require_once statement. This is preferable to an include statement, as it will generate a fatal error if the file is not found\u2014<br>and believe me, not finding the file containing the login details to your database is a fatal error.<br>Also, using require_once instead of require means that the file will be read in only when it has not previously been included, which prevents wasteful duplicate disk accesses.<\/p>\n\n\n\n<p><a href=\"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/data\/connect.txt\" data-type=\"URL\" data-id=\"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/data\/connect.txt\">Connecting to a MySQL server with mysqli<\/a><\/p>\n\n\n\n<p><strong>&lt;?php<br>require_once &#8216;login.php&#8217;;<br>$conn = new mysqli($hn, $un, $pw, $db);<br>if ($conn-&gt;connect_error) die(&#8220;Fatal Error&#8221;);<br>?&gt;<\/strong><\/p>\n\n\n\n<p>This example creates a new object called $conn by calling a new instance of the mysqli method, passing all the values retrieved from the login.php file. We achieve error checking by referencing the  $conn-&gt;connect_error property.<\/p>\n\n\n\n<p>The -&gt; operator indicates that the item on the right is a property or method of the object on the left. In this case, if connect_error has a value, there was an error, so we call the die function to terminate the program.<br>The $conn object is used in the following examples to access the MySQL database.<\/p>\n\n\n\n<p>The die function is great when you are developing PHP code, but<br>of course you will want more user-friendly error messages on a<br>production server. In this case, you won\u2019t abort your PHP program,<br>but will format a message that will be displayed when the program<br>exits normally\u2014perhaps something like <a href=\"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/data\/fatal_error.txt\">this<\/a>:<\/p>\n\n\n\n<p><strong>function mysql_fatal_error()<br>{<br>echo &lt;&lt;&lt; _END<br>We are sorry, but it was not possible to complete<br>the requested task. The error message we got was:<br>Fatal Error<br>Please click the back button on your browser<br>and try again. If you are still having problems,<br>please email our administrator. Thank you.<br>_END;<br>}<\/strong><\/p>\n\n\n\n<p>You should also never be tempted to output the contents of any<br>error message received from MySQL. Rather than helping your<br>users, you could give away sensitive information to hackers such as<br>login details. Instead, just guide the user with information on how<br>to overcome their difficulty based on what the error message<br>reports to your code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that you have saved the login.php file, you can include it in any PHP files that will need to access the database by using the require_once statement. This is preferable to an include statement, as it will generate a fatal error if the file is not found\u2014and believe me, not finding the file containing &hellip; <a href=\"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/2021\/11\/29\/connecting-to-a-mysql-database\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Connecting to a MySQL 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\/383"}],"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=383"}],"version-history":[{"count":5,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/383\/revisions"}],"predecessor-version":[{"id":397,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/383\/revisions\/397"}],"wp:attachment":[{"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/tietokanta.dy.fi:8243\/mikko\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}