{"id":369,"date":"2018-01-15T01:30:31","date_gmt":"2018-01-15T01:30:31","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=369"},"modified":"2018-01-15T01:30:31","modified_gmt":"2018-01-15T01:30:31","slug":"connect-to-the-mysqli-database","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/connect-to-the-mysqli-database\/","title":{"rendered":"Connect to the Mysqli database"},"content":{"rendered":"<p>When you are using php you first need to connect to the mysqli database by using simple php query. <\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n$servername = &quot;localhost&quot;;<br \/>\n$username = &quot;username&quot;;<br \/>\n$password = &quot;password&quot;;<br \/>\n$database = &quot;databasename&quot;;<\/p>\n<p>\/\/ Create connection<br \/>\n$conn = new mysqli($servername, $username, $password, $database);<\/p>\n<p>\/\/ Check connection<br \/>\nif ($conn-&gt;connect_error) {<br \/>\n    die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);<br \/>\n}<br \/>\necho &quot;Connected successfully&quot;;<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>In the above php query, we have first declared the variables <code>$servername, $username, $password, $database<\/code>. This is the login information to connect the mysqli database. <\/p>\n<p>To connect we will call the php function <code>mysqli()<\/code> <\/p>\n<p><code>$conn = new mysqli($servername, $username, $password, $database);<\/code><\/p>\n<p>We created a variable called <code>$conn<\/code> and trying to connect the database by using our variable details.<\/p>\n<p>On next step, we are verifying the connection by using if statement.<\/p>\n<p>[php]if ($conn-&gt;connect_error) {<br \/>\n    die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);<br \/>\n} [\/php]<\/p>\n<p>In the if statement we are checking if the connection is successful or not by using the <code>connect_error<\/code> condition. if it fails to connect then our <code>die()<\/code> error function will be a call to show the message &#8220;<code>Connection failed<\/code>&#8221; and it will show the error message.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you are using php you first need to connect to the mysqli database by using simple php query. Example: [php]&lt;?php $servername = &quot;localhost&quot;; $username<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-369","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/comments?post=369"}],"version-history":[{"count":1,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/369\/revisions"}],"predecessor-version":[{"id":370,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/369\/revisions\/370"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}