{"id":2124,"date":"2014-12-30T06:37:29","date_gmt":"2014-12-30T06:37:29","guid":{"rendered":"http:\/\/www.vishmax.com\/en\/?p=2124"},"modified":"2014-12-30T06:37:29","modified_gmt":"2014-12-30T06:37:29","slug":"php-mysql-create-insert-select-print-update-and-delete","status":"publish","type":"post","link":"https:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/","title":{"rendered":"PHP MySQL create, Insert, select, print, update and delete"},"content":{"rendered":"<p>PHP is a well known server side programming language which is used for web development. PHP language development began in 1994 by Rasmus Lerdorf, a Greenland programmer. On 28 August 2014 PHP 5.6 version has been released. <\/p>\n<p>MySQL is one of the most popular database which is developed as open source by Oracle Corporation and it is written in C and C++. Using PHP with MySQL is a good combination for web apps and today most of the web applications are using this combination.<\/p>\n<p>In this topic we cover the basics of PHP language that is Create, Insert, Select and Delete. This post is especially for the beginner in web development. <\/p>\n<h2>How to create MySQL database<\/h2>\n<p>We can create a MySQL database using &#8216;<strong>CREATE DATABASE<\/strong>&#8216; statement. Below example will show how to create a database using query.<\/p>\n<pre lang=\"html\">\r\n\r\nCREATE DATABASE max_demo\r\n\r\n<\/pre>\n<p>the above query will create a MySQl database called &#8220;max_demo&#8221;<\/p>\n<h2>How to create a MySQL table in database<\/h2>\n<p>The <strong>CREATE TABLE<\/strong> statement is used to create a table in MySQL. <\/p>\n<p>In our example we are going to create a table in database. Below query will create a table named &#8220;users&#8221;, with five columns: &#8220;id&#8221;, &#8220;firstname&#8221;, &#8220;lastname&#8221;, &#8220;email&#8221; and &#8220;reg_date&#8221;.<\/p>\n<pre lang=\"php\">\r\n\r\nCREATE TABLE users (\r\nid INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,\r\nfirstname VARCHAR(30) NOT NULL,\r\nlastname VARCHAR(30) NOT NULL,\r\nemail VARCHAR(80),\r\nreg_date TIMESTAMP\r\n)\r\n\r\n<\/pre>\n<h2>How to insert data Into MySQL<\/h2>\n<p>For inserting we can use <strong>INSERT INTO<\/strong>  statement. Below is an example of inserting data into MySQL. <\/p>\n<p>INSERT INTO table_name (column1, column2, column3,&#8230;)<br \/>\nVALUES (value1, value2, value3,&#8230;)<\/p>\n<pre lang=\"php\">\r\n\r\n$sql = \"INSERT INTO users (firstname, lastname, email)\r\nVALUES ('Ajay', 'AK', 'ajay@vishmax.com')\";\r\n\r\n<\/pre>\n<h2>How to select data from a MySQL Database<\/h2>\n<p>The <strong>SELECT<\/strong> statement is used to select data from one or more tables.<\/p>\n<pre lang=\"php\">\r\nSELECT column_name(s) FROM table_name\r\n<\/pre>\n<p>or you can select entair columns by using <strong>*<\/strong> character.<\/p>\n<pre lang=\"php\">\r\nSELECT * FROM table_name\r\n<\/pre>\n<p>In our working example we can select the data by following way.<\/p>\n<pre lang=\"php\">\r\n\r\n$sql = \"SELECT id, firstname, lastname FROM users\";\r\n$result = $conn->query($sql);\r\n\r\n<\/pre>\n<h2>How to print data from a MySQL Database<\/h2>\n<p>For printing data we can use <strong>echo<\/strong> statement.<\/p>\n<pre lang=\"php\">\r\n\r\n\/\/ Add your connection details here\r\n\r\n$sql = \"SELECT id, firstname, lastname FROM users\";\r\n$result = mysqli_query($conn, $sql);\r\n\r\nif (mysqli_num_rows($result) > 0) {\r\n    \/\/ output data of each row\r\n    while($row = mysqli_fetch_assoc($result)) {\r\n        echo \"id: \" . $row[\"id\"]. \" - Name: \" . $row[\"firstname\"]. \" \" . $row[\"lastname\"]. \"<br>\";\r\n    }\r\n} else {\r\n    echo \"0 results found\";\r\n}\r\n<\/pre>\n<h2>How to update data from MySQL database<\/h2>\n<p>The <strong>UPDATE<\/strong> statement is used to update existing records in a table. Below query will update the last name of the user record with id=1<\/p>\n<pre lang=\"php\">\r\n\r\n\/\/ Add your connection details here\r\n\r\n$sql = \"UPDATE users SET lastname='Rao' WHERE id=1\";\r\n\r\n<\/pre>\n<h2>How to delete data from MySQL database<\/h2>\n<p>The <strong>DELETE<\/strong> statement is used to delete records from a table. Below query will delete the data from database, where user record with id=1<\/p>\n<pre lang=\"php\">\r\n\r\n\/\/ Add your connection details here\r\n\r\n$sql = \"DELETE FROM users WHERE id=1\";\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP is a well known server side programming language which is used for web development. PHP language development began in 1994 by Rasmus Lerdorf, a Greenland programmer. On 28 August 2014 PHP 5.6 version has been released. MySQL is one of the most popular database which is developed as open source by Oracle Corporation and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[44],"tags":[72,48],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.2.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP MySQL create, Insert, select, print, update and delete - Vishmax.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP MySQL create, Insert, select, print, update and delete - Vishmax.com\" \/>\n<meta property=\"og:description\" content=\"PHP is a well known server side programming language which is used for web development. PHP language development began in 1994 by Rasmus Lerdorf, a Greenland programmer. On 28 August 2014 PHP 5.6 version has been released. MySQL is one of the most popular database which is developed as open source by Oracle Corporation and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/\" \/>\n<meta property=\"og:site_name\" content=\"Vishmax.com\" \/>\n<meta property=\"article:published_time\" content=\"2014-12-30T06:37:29+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\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\":\"Article\",\"@id\":\"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/785fa94b56eb17ae7706d30777d6ab93\"},\"headline\":\"PHP MySQL create, Insert, select, print, update and delete\",\"datePublished\":\"2014-12-30T06:37:29+00:00\",\"dateModified\":\"2014-12-30T06:37:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/\"},\"wordCount\":358,\"publisher\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/#organization\"},\"keywords\":[\"Code Library\",\"PHP\"],\"articleSection\":[\"Labs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/\",\"url\":\"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/\",\"name\":\"PHP MySQL create, Insert, select, print, update and delete - Vishmax.com\",\"isPartOf\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/#website\"},\"datePublished\":\"2014-12-30T06:37:29+00:00\",\"dateModified\":\"2014-12-30T06:37:29+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.vishmax.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP MySQL create, Insert, select, print, update and delete\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#website\",\"url\":\"http:\/\/www.vishmax.com\/en\/\",\"name\":\"Vishmax.com\",\"description\":\"Software Company in Calicut for Web Design and eCommerce\",\"publisher\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.vishmax.com\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#organization\",\"name\":\"Vishmax.com\",\"url\":\"http:\/\/www.vishmax.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2023\/03\/logo-vishmax.png\",\"contentUrl\":\"https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2023\/03\/logo-vishmax.png\",\"width\":150,\"height\":50,\"caption\":\"Vishmax.com\"},\"image\":{\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/785fa94b56eb17ae7706d30777d6ab93\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e218268870406c917d20b442936d9e68?s=96&d=monsterid&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e218268870406c917d20b442936d9e68?s=96&d=monsterid&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/www.vishmax.com\/en\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP MySQL create, Insert, select, print, update and delete - Vishmax.com","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":"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/","og_locale":"en_US","og_type":"article","og_title":"PHP MySQL create, Insert, select, print, update and delete - Vishmax.com","og_description":"PHP is a well known server side programming language which is used for web development. PHP language development began in 1994 by Rasmus Lerdorf, a Greenland programmer. On 28 August 2014 PHP 5.6 version has been released. MySQL is one of the most popular database which is developed as open source by Oracle Corporation and [&hellip;]","og_url":"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/","og_site_name":"Vishmax.com","article_published_time":"2014-12-30T06:37:29+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/#article","isPartOf":{"@id":"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/"},"author":{"name":"admin","@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/785fa94b56eb17ae7706d30777d6ab93"},"headline":"PHP MySQL create, Insert, select, print, update and delete","datePublished":"2014-12-30T06:37:29+00:00","dateModified":"2014-12-30T06:37:29+00:00","mainEntityOfPage":{"@id":"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/"},"wordCount":358,"publisher":{"@id":"http:\/\/www.vishmax.com\/en\/#organization"},"keywords":["Code Library","PHP"],"articleSection":["Labs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/","url":"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/","name":"PHP MySQL create, Insert, select, print, update and delete - Vishmax.com","isPartOf":{"@id":"http:\/\/www.vishmax.com\/en\/#website"},"datePublished":"2014-12-30T06:37:29+00:00","dateModified":"2014-12-30T06:37:29+00:00","breadcrumb":{"@id":"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.vishmax.com\/en\/labs\/php-mysql-create-insert-select-print-update-and-delete\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.vishmax.com\/en\/"},{"@type":"ListItem","position":2,"name":"PHP MySQL create, Insert, select, print, update and delete"}]},{"@type":"WebSite","@id":"http:\/\/www.vishmax.com\/en\/#website","url":"http:\/\/www.vishmax.com\/en\/","name":"Vishmax.com","description":"Software Company in Calicut for Web Design and eCommerce","publisher":{"@id":"http:\/\/www.vishmax.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.vishmax.com\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/www.vishmax.com\/en\/#organization","name":"Vishmax.com","url":"http:\/\/www.vishmax.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2023\/03\/logo-vishmax.png","contentUrl":"https:\/\/www.vishmax.com\/en\/wp-content\/uploads\/2023\/03\/logo-vishmax.png","width":150,"height":50,"caption":"Vishmax.com"},"image":{"@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/785fa94b56eb17ae7706d30777d6ab93","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vishmax.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e218268870406c917d20b442936d9e68?s=96&d=monsterid&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e218268870406c917d20b442936d9e68?s=96&d=monsterid&r=g","caption":"admin"},"url":"https:\/\/www.vishmax.com\/en\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/posts\/2124"}],"collection":[{"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/comments?post=2124"}],"version-history":[{"count":0,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/posts\/2124\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/media?parent=2124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/categories?post=2124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vishmax.com\/en\/wp-json\/wp\/v2\/tags?post=2124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}