{"id":877,"date":"2023-04-27T06:10:01","date_gmt":"2023-04-27T10:10:01","guid":{"rendered":"https:\/\/www.searchenginegenie.com\/programming-blog\/?p=877"},"modified":"2023-04-27T06:12:01","modified_gmt":"2023-04-27T10:12:01","slug":"build-a-mysql-database-using-php%ef%bf%bc","status":"publish","type":"post","link":"https:\/\/www.searchenginegenie.com\/programming-blog\/build-a-mysql-database-using-php%ef%bf%bc\/","title":{"rendered":"Build a MySQL Database Using PHP"},"content":{"rendered":"\n<p>1. Create a MySQL database:<\/p>\n\n\n\n<p>CREATE DATABASE mydatabase;<\/p>\n\n\n\n<p>2. Create a MySQL user and grant access to the database:<\/p>\n\n\n\n<p>CREATE USER &#8216;myuser&#8217;@&#8217;localhost&#8217; IDENTIFIED BY &#8216;mypassword&#8217;;<\/p>\n\n\n\n<p>GRANT ALL PRIVILEGES ON mydatabase.* TO &#8216;myuser&#8217;@&#8217;localhost&#8217;;<\/p>\n\n\n\n<p>3. Create a PHP file and establish a connection to the MySQL database:<\/p>\n\n\n\n<p>&lt;?php<\/p>\n\n\n\n<p>$servername = &#8220;localhost&#8221;;<\/p>\n\n\n\n<p>$username = &#8220;myuser&#8221;;<\/p>\n\n\n\n<p>$password = &#8220;mypassword&#8221;;<\/p>\n\n\n\n<p>$dbname = &#8220;mydatabase&#8221;;<\/p>\n\n\n\n<p>$conn = new mysqli($servername, $username, $password, $dbname);<\/p>\n\n\n\n<p>if ($conn-&gt;connect_error) {<\/p>\n\n\n\n<p>   die(&#8220;Connection failed: &#8221; . $conn-&gt;connect_error);<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>echo &#8220;Connected successfully&#8221;;<\/p>\n\n\n\n<p>?&gt;<\/p>\n\n\n\n<p>4. Create a table in the database:<\/p>\n\n\n\n<p>CREATE TABLE users (<\/p>\n\n\n\n<p>  id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,<\/p>\n\n\n\n<p>  firstname VARCHAR(30) NOT NULL,<\/p>\n\n\n\n<p>  lastname VARCHAR(30) NOT NULL,<\/p>\n\n\n\n<p>  email VARCHAR(50),<\/p>\n\n\n\n<p>  reg_date TIMESTAMP<\/p>\n\n\n\n<p>);<\/p>\n\n\n\n<p>5. Insert data into the table:<\/p>\n\n\n\n<p>INSERT INTO users (firstname, lastname, email)<\/p>\n\n\n\n<p>VALUES (&#8216;John&#8217;, &#8216;Doe&#8217;, &#8216;john@example.com&#8217;);<\/p>\n\n\n\n<p>6. Retrieve data from the table:<\/p>\n\n\n\n<p>SELECT * FROM users;<\/p>\n\n\n\n<p>7. Close the connection:<\/p>\n\n\n\n<p>$conn-&gt;close();<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Create a MySQL database: CREATE DATABASE mydatabase; 2. Create a MySQL user and grant access to the database: CREATE USER &#8216;myuser&#8217;@&#8217;localhost&#8217; IDENTIFIED BY &#8216;mypassword&#8217;; GRANT ALL PRIVILEGES ON mydatabase.* TO &#8216;myuser&#8217;@&#8217;localhost&#8217;; 3. Create a PHP file and establish a connection to the MySQL database: &lt;?php $servername = &#8220;localhost&#8221;; $username = &#8220;myuser&#8221;; $password = &#8220;mypassword&#8221;; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,1],"tags":[],"class_list":["post-877","post","type-post","status-publish","format-standard","hentry","category-php","category-programming"],"_links":{"self":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/comments?post=877"}],"version-history":[{"count":2,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/877\/revisions"}],"predecessor-version":[{"id":879,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/877\/revisions\/879"}],"wp:attachment":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/media?parent=877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/categories?post=877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/tags?post=877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}