<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":4497,"date":"2022-07-28T05:18:54","date_gmt":"2022-07-28T05:18:54","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=4497"},"modified":"2022-07-28T05:37:33","modified_gmt":"2022-07-28T05:37:33","slug":"how-to-create-a-database-using-php-and-mysql","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/","title":{"rendered":"How to create a database using PHP and MySQL ?"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-4504\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1.png\" alt=\"\" width=\"882\" height=\"487\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1.png 882w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1-300x166.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1-768x424.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1-390x215.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1-820x453.png 820w\" sizes=\"(max-width: 882px) 100vw, 882px\" \/><\/p>\n<ul>\n<li style=\"text-align: justify;\">Database is a collection of inter-related data which helps in insertion, retrieval and deletion of data from database and organizes the data in the form of views, tables, reports, schemas etc.<\/li>\n<li style=\"text-align: justify;\">For example, institute database organizes the data about students, admin, faculty, and staff etc. which helps in insertion, efficient retrieval and deletion of data from it.<\/li>\n<li style=\"text-align: justify;\">In MySQL we need to create a database to execute a query.<\/li>\n<li style=\"text-align: justify;\">MySQL is an open-source relational database management system and it is distributed, supported and developed by Oracle Corporation.<\/li>\n<li style=\"text-align: justify;\">MySQL is a database system that runs on a server and it is ideal for both small and large applications.<\/li>\n<li style=\"text-align: justify;\">From <a href=\"https:\/\/www.wikitechy.com\/php\/\">PHP<\/a> script establish a connection to MySQL server.<\/li>\n<li style=\"text-align: justify;\">Write a <a href=\"https:\/\/www.wikitechy.com\/tutorials\/sql\/\">SQL<\/a> query to create a database and store it in a string variable then execute the queries.<\/li>\n<li style=\"text-align: justify;\">Using <strong>Object-oriented<\/strong> procedure, we can use the query () function of MySQLi class to execute our query.<\/li>\n<li style=\"text-align: justify;\">Using <strong>procedural<\/strong> procedure, we can use the mysqli_query () function of PHP to execute our query.<\/li>\n<li style=\"text-align: justify;\">Using <strong>PDO<\/strong> procedure then we can execute our query.<\/li>\n<\/ul>\n<h2 id=\"sample-code\">Sample Code<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/>$servername = &quot;localhost&quot;;<br\/>$username = &quot;username&quot;;<br\/>$password = &quot;password&quot;;<br\/><br\/>\/\/ Creating a connection<br\/>$conn = new mysqli($servername, $username, $password);<br\/>\/\/ Check connection<br\/>if ($conn-&gt;connect_error) {<br\/>    die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);<br\/>} <br\/>\/\/ Creating a database named newDB<br\/>$sql = &quot;CREATE DATABASE newDB&quot;;<br\/>if ($conn-&gt;query($sql) === TRUE) {<br\/>    echo &quot;Database created successfully with the name newDB&quot;;<br\/>} else {<br\/>    echo &quot;Error creating database: &quot; . $conn-&gt;error;<br\/>}<br\/><br\/>\/\/ closing connection<br\/>$conn-&gt;close();<br\/>?&gt;<\/code><\/pre> <\/div>\n<h2 id=\"output\">Output<\/h2>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-4498\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-mysql.png\" alt=\"\" width=\"1180\" height=\"625\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-mysql.png 1180w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-mysql-300x159.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-mysql-1024x542.png 1024w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-mysql-768x407.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-mysql-390x207.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-mysql-820x434.png 820w\" sizes=\"(max-width: 1180px) 100vw, 1180px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Database is a collection of inter-related data which helps in insertion, retrieval and deletion of data from database and organizes the data in the form of views, tables, reports, schemas etc. For example, institute database organizes the data about students, admin, faculty, and staff etc. which helps in insertion, efficient retrieval and deletion of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[8182],"tags":[17592,17589,17585,17582,17583,17591,17593,17586,17584,17587,17590,17581,17588],"class_list":["post-4497","post","type-post","status-publish","format-standard","hentry","category-php","tag-accessing-the-database-in-php","tag-create-database-mysql","tag-create-mysql-database-and-table-using-php-in-xampp","tag-create-mysql-database-using-php","tag-creating-mysql-database-using-php","tag-database-connection-in-php-with-mysql","tag-how-can-we-connect-to-a-mysql-database-from-a-php-script","tag-how-to-connect-mysql-database-with-php-website","tag-how-to-create-a-database-in-mysql-using-php-script","tag-mysql-database-in-php","tag-php-code-to-create-database-and-table","tag-php-mysql-create-database","tag-selecting-a-database-in-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to create a database using PHP and MySQL ? - PHP - Wikitechy<\/title>\n<meta name=\"description\" content=\"How to create a database using PHP and MySQL ? - Database is a collection of inter-related data which helps in insertion, retrieval and deletion of data from database\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to create a database using PHP and MySQL ? - PHP - Wikitechy\" \/>\n<meta property=\"og:description\" content=\"How to create a database using PHP and MySQL ? - Database is a collection of inter-related data which helps in insertion, retrieval and deletion of data from database\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-28T05:18:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-28T05:37:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1.png\" \/>\n<meta name=\"author\" content=\"webmaster\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"webmaster\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/\",\"name\":\"How to create a database using PHP and MySQL ? - PHP - Wikitechy\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1.png\",\"datePublished\":\"2022-07-28T05:18:54+00:00\",\"dateModified\":\"2022-07-28T05:37:33+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4\"},\"description\":\"How to create a database using PHP and MySQL ? - Database is a collection of inter-related data which helps in insertion, retrieval and deletion of data from database\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/#primaryimage\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1.png\",\"contentUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1.png\",\"width\":882,\"height\":487},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4\",\"name\":\"webmaster\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"caption\":\"webmaster\"},\"sameAs\":[\"https:\/\/www.wikitechy.com\/interview-questions\"],\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/webmaster\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to create a database using PHP and MySQL ? - PHP - Wikitechy","description":"How to create a database using PHP and MySQL ? - Database is a collection of inter-related data which helps in insertion, retrieval and deletion of data from database","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/","og_locale":"en_US","og_type":"article","og_title":"How to create a database using PHP and MySQL ? - PHP - Wikitechy","og_description":"How to create a database using PHP and MySQL ? - Database is a collection of inter-related data which helps in insertion, retrieval and deletion of data from database","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/","og_site_name":"Wikitechy","article_published_time":"2022-07-28T05:18:54+00:00","article_modified_time":"2022-07-28T05:37:33+00:00","og_image":[{"url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1.png"}],"author":"webmaster","twitter_card":"summary_large_image","twitter_misc":{"Written by":"webmaster","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/","name":"How to create a database using PHP and MySQL ? - PHP - Wikitechy","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1.png","datePublished":"2022-07-28T05:18:54+00:00","dateModified":"2022-07-28T05:37:33+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4"},"description":"How to create a database using PHP and MySQL ? - Database is a collection of inter-related data which helps in insertion, retrieval and deletion of data from database","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-create-a-database-using-php-and-mysql\/#primaryimage","url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1.png","contentUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/07\/php-database-connection-1.png","width":882,"height":487},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4","name":"webmaster","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","caption":"webmaster"},"sameAs":["https:\/\/www.wikitechy.com\/interview-questions"],"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/webmaster\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=4497"}],"version-history":[{"count":5,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4497\/revisions"}],"predecessor-version":[{"id":4505,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4497\/revisions\/4505"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=4497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=4497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=4497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}