{"id":373,"date":"2018-01-15T02:06:19","date_gmt":"2018-01-15T02:06:19","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=373"},"modified":"2018-01-15T02:06:19","modified_gmt":"2018-01-15T02:06:19","slug":"create-a-mysql-table-using-php","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/create-a-mysql-table-using-php\/","title":{"rendered":"Create a MySQL Table using php"},"content":{"rendered":"<p>You can use a php script to create MySQL table.<\/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$dbname = &quot;myDB&quot;;<\/p>\n<p>\/\/ Create connection<br \/>\n$conn = new mysqli($servername, $username, $password, $dbname);<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>\/\/ sql to create table<br \/>\n$sql = &quot;CREATE TABLE MyGuests (<br \/>\nid INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,<br \/>\nfirstname VARCHAR(30) NOT NULL,<br \/>\nlastname VARCHAR(30) NOT NULL,<br \/>\nemail VARCHAR(50),<br \/>\nreg_date TIMESTAMP<br \/>\n)&quot;;<\/p>\n<p>if ($conn-&gt;query($sql) === TRUE) {<br \/>\n    echo &quot;Table MyGuests created successfully&quot;;<br \/>\n} else {<br \/>\n    echo &quot;Error creating table: &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>First, we have to use the code to connect the server.<\/p>\n<p><code>$servername = \"localhost\";<br \/>\n$username = \"username\";<br \/>\n$password = \"password\";<br \/>\n$dbname = \"myDB\";<\/p>\n<p>\/\/ Create connection<br \/>\n$conn = new mysqli($servername, $username, $password, $dbname);<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 connection is successfully establish the next step is creating a MySQL table. To do that we will run the MySQL query.<\/p>\n<p><code><br \/>\n$sql = \"CREATE TABLE MyGuests (<br \/>\nid INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,<br \/>\nfirstname VARCHAR(30) NOT NULL,<br \/>\nlastname VARCHAR(30) NOT NULL,<br \/>\nemail VARCHAR(50),<br \/>\nreg_date TIMESTAMP<br \/>\n)\";<\/code><\/p>\n<p>In the $sql query we are asking php script to <code>CREATE TABLE myGuests<\/code>. In myGuest table, we will be having different columns. <\/p>\n<p><code>id<\/code> &#8211; The id represents the integer value means it will hold the numbers. We have assigned the AUTO_INCREMENT condition which will increase the number by 1 when new entry added to the database. It has given the value PRIMARY KEY which means it will be always unique. You can add duplicate value to the PRIMARY Key.<\/p>\n<p><code>firstname<\/code>  &#8211; In this column, we will be adding text value which will consider the VARCHAR. The NOT NULL condition says you can&#8217;t keep it blank. There has to be some value in this field.<\/p>\n<p><code>lastname  <\/code>&#8211; In this column similar to the firstname we have instructed the mysql to give the condition VARCHAR with NOT NULL value.<\/p>\n<p><code>email  <\/code>&#8211; The email field has the VARCHAR value with no special condition. It means it can be run without adding any value to it.<\/p>\n<p><code>reg_date <\/code>&#8211; Is the statement to add default date to the column.<\/p>\n<p>In the next line of php, we are checking if the connection is successfully achieve by using if statement.<\/p>\n<p><code>if ($conn->query($sql) === TRUE) {<br \/>\n    echo \"Table MyGuests created successfully\";<br \/>\n} else {<br \/>\n    echo \"Error creating table: \" . $conn->error;<br \/>\n}<\/code><\/p>\n<p>In the if statement we are checking both the connection <code>$conn<\/code> which represents the server connection and <code>query($sql)<\/code> represents the mysql connection. The identical check operator is used to see whether the value is TRUE or not. According to the outcome, the echo statement is generated.<\/p>\n<p>If everything goes well as per the script then you will see a new entry in your mysql table.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use a php script to create MySQL table. Example. [php]&lt;?php $servername = &quot;localhost&quot;; $username = &quot;username&quot;; $password = &quot;password&quot;; $dbname = &quot;myDB&quot;; \/\/<\/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-373","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/373","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=373"}],"version-history":[{"count":1,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/373\/revisions"}],"predecessor-version":[{"id":374,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/373\/revisions\/374"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}