{"id":371,"date":"2018-01-15T01:40:23","date_gmt":"2018-01-15T01:40:23","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=371"},"modified":"2018-01-15T01:40:23","modified_gmt":"2018-01-15T01:40:23","slug":"create-a-mysql-database","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/create-a-mysql-database\/","title":{"rendered":"Create a MySQL Database"},"content":{"rendered":"<p>You can create a mysql database by using the php script.<\/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;;<\/p>\n<p>\/\/ Create connection<br \/>\n$conn = new mysqli($servername, $username, $password);<br \/>\n\/\/ Check connection<br \/>\nif ($conn-&gt;connect_error) {<br \/>\n    die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);<br \/>\n} <\/p>\n<p>\/\/ Create database<br \/>\n$sql = &quot;CREATE DATABASE myDB&quot;;<br \/>\nif ($conn-&gt;query($sql) === TRUE) {<br \/>\n    echo &quot;Database created successfully&quot;;<br \/>\n} else {<br \/>\n    echo &quot;Error creating database: &quot; . $conn-&gt;error;<br \/>\n}<\/p>\n<p>$conn-&gt;close();<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code explanation:<\/strong><\/p>\n<p>In the code first, we are connecting php to the server by using server login details.<\/p>\n<p><code>$servername = \"localhost\";<br \/>\n$username = \"username\";<br \/>\n$password = \"password\";<\/p>\n<p>\/\/ Create connection<br \/>\n$conn = new mysqli($servername, $username, $password);<br \/>\n\/\/ Check connection<br \/>\nif ($conn->connect_error) {<br \/>\n    die(\"Connection failed: \" . $conn->connect_error);<br \/>\n} <\/code><\/p>\n<p>Once the server is connection we have used the mysql query statement to create a database on the server.<\/p>\n<p><code>$sql = \"CREATE DATABASE myDB\";<\/code><\/p>\n<p>To verify it we are using if statement. The if statement checks whether the connection to the server is successfully achieved by using the <code>$conn<\/code> variable. Then it checks the <code>$sql<\/code> query with the operator identical check <code>===<\/code> is true. If everything is run ok the echo statement &#8220;Database created successfully&#8221; is proceed to the browser else the error message &#8220;Error creating the database: &#8221; with the connection error. The <code>$conn->error<\/code> is shown to the browser.<\/p>\n<p><code>if ($conn->query($sql) === TRUE) {<br \/>\n    echo \"Database created successfully\";<br \/>\n} else {<br \/>\n    echo \"Error creating database: \" . $conn->error;<br \/>\n}<\/code><\/p>\n<p>In the end, we are closing the php script with the closing function.<\/p>\n<p><code>$conn->close();<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can create a mysql database by using the php script. Example: [php]&lt;?php $servername = &quot;localhost&quot;; $username = &quot;username&quot;; $password = &quot;password&quot;; \/\/ Create connection<\/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-371","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/371","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=371"}],"version-history":[{"count":1,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/371\/revisions"}],"predecessor-version":[{"id":372,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/371\/revisions\/372"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}