{"id":1948,"date":"2019-09-18T12:00:00","date_gmt":"2019-09-18T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/how-to-write-a-rest-api-in-php-and-mysql-step-by-step\/"},"modified":"2026-07-06T18:42:47","modified_gmt":"2026-07-06T16:42:47","slug":"how-to-write-a-rest-api-in-php-and-mysql-step-by-step","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-write-a-rest-api-in-php-and-mysql-step-by-step\/","title":{"rendered":"How to Write a REST API in PHP and MySQL (Step by Step)"},"content":{"rendered":"<p>In this article, I will teach you how to build a REST web service that would connect to a MySQL database. Then it would fetch records from MySQL table and return them as json. Finally, we would host this service in IIS (Internet Information Service on Windows 10)<\/p>\n<p>Let&#8217;s go!<\/p>\n<p>These are the steps we would follow:<\/p>\n<ol>\n<li><a href=\"#t1\">Make sure IIS is installed and tested<\/a><\/li>\n<li><a href=\"#t2\">Install and test PHP<\/a><\/li>\n<li><a href=\"#t3\">Create a MySQL database and table<\/a><\/li>\n<li><a href=\"#t4\">Write the REST API<\/a><\/li>\n<li><a href=\"#t5\">Create an Application in IIS<\/a><\/li>\n<li><a href=\"#t6\">Deploy and Test the API<\/a><\/li>\n<li><a href=\"#t7\">Next Steps<\/a><\/li>\n<\/ol>\n<p><a href=\"https:\/\/kindsonthegenius.com\/blog\/how-to-write-a-rest-api-in-php-and-mysql-step-by-step\/\">This is How to Build REST API in .Net using Visual Studio<\/a><\/p>\n<p><strong id=\"t1\">1. Make sure IIS is installed and tested<\/strong><\/p>\n<p>To test our PHP API, we need to host it in a server. Since we are using Windows, it is likely, you already have IIS.To check if IIS is installed:<\/p>\n<ul>\n<li>Open Programs and Features<\/li>\n<li>Make sure IIS and other features are selected as shown below<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Intalling-Internet-Information-Services.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1230 \" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Intalling-Internet-Information-Services.jpg\" alt=\"Installing Internet Information Services\" width=\"622\" height=\"518\" \/><\/a><\/p>\n<ul>\n<li>To test the installation, just open the browser and visit http:\/\/localhost. If it is installed ok, then you will see the page below;<\/li>\n<\/ul>\n<figure id=\"attachment_1231\" aria-describedby=\"caption-attachment-1231\" style=\"width: 735px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/IIS-Start-page.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1231 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/IIS-Start-page-1024x555.jpg\" alt=\"IIS Start page\" width=\"735\" height=\"398\" \/><\/a><figcaption id=\"caption-attachment-1231\" class=\"wp-caption-text\">IIS Start page<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p><strong id=\"t2\">2. Install and test PHP<\/strong><\/p>\n<p>Simply download PHP from this <a href=\"https:\/\/windows.php.net\/download\/\" target=\"_blank\" rel=\"noopener\">link<\/a>.<\/p>\n<p>Unzip it into a folder C:\/php<\/p>\n<p>To test the php installation:<\/p>\n<p>Create a test file test.php. Write the following code inside<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\n   <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"color: #007020;\">phpinfo<\/span>()<span style=\"color: #333333;\">:<\/span>\n<span style=\"color: #557799;\">?&gt;<\/span>\n<\/pre>\n<p>Save it in in\u00a0 this directory C:\/inetpub\/wwwroot<\/p>\n<p>Open the browser and visit http:\/\/localhost\/test.php. If you see the page below, then the installation was successful<\/p>\n<p><strong><span style=\"color: #003300;\">Use Web Platform Installer<\/span><\/strong>: You can also install PHP using <a href=\"https:\/\/www.microsoft.com\/web\/downloads\/platform.aspx\" target=\"_blank\" rel=\"noopener noreferrer\">Web Platform Installer<\/a>. Download it from here. Install and run it. Then install PHP following the instruction.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t3\">3. Create a MySQL database and table<\/strong><\/p>\n<p>I&#8217;m sure you already have MySQL installed. If not, download it from <a href=\"https:\/\/dev.mysql.com\/downloads\/file\/?id=487686\" target=\"_blank\" rel=\"noopener noreferrer\">here(the community edition)<\/a> and install.<\/p>\n<p>Create a table called tblUsers with the following fields:<\/p>\n<p>id, firstname, lastname, location<\/p>\n<p>Insert a few records into the table<\/p>\n<p>To be able to connect to your MySQL server from an API, run the query below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">ALTER<\/span> USER <span style=\"background-color: #fff0f0;\">'root'<\/span><span style=\"color: #333333;\">@<\/span><span style=\"background-color: #fff0f0;\">'localhost'<\/span> \nIDENTIFIED <span style=\"color: #008800; font-weight: bold;\">WITH<\/span> mysql_native_password\n<span style=\"color: #008800; font-weight: bold;\">BY<\/span> <span style=\"background-color: #fff0f0;\">'root'<\/span>;\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong id=\"t4\">4. Write the REST API<\/strong><\/p>\n<p>To write the REST API, you will need an editor to make it easier. You can used Notepad++. <a href=\"https:\/\/notepad-plus-plus.org\/downloads\/v7.7.1\/\" target=\"_blank\" rel=\"noopener noreferrer\">Download it here.<\/a><\/p>\n<p>Create a file called services.php. Write the following code inside<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\n   <span style=\"color: #996633;\">$url<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"localhost:3301\"<\/span>;\n   <span style=\"color: #996633;\">$database<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"dcsdb\"<\/span>;\n   <span style=\"color: #996633;\">$username<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"root\"<\/span>;\n   <span style=\"color: #996633;\">$password<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"*****\"<\/span>;\t\n\n   <span style=\"color: #996633;\">$conn<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">mysqli_connect<\/span>(<span style=\"color: #996633;\">$url<\/span>, <span style=\"color: #996633;\">$username<\/span>, <span style=\"color: #996633;\">$password<\/span>, <span style=\"color: #996633;\">$database<\/span>);\n   <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #333333;\">!<\/span><span style=\"color: #996633;\">$conn<\/span>) {\n\t<span style=\"color: #008800; font-weight: bold;\">die<\/span>(<span style=\"background-color: #fff0f0;\">\"Unable to connect: \"<\/span> <span style=\"color: #333333;\">.<\/span> <span style=\"color: #996633;\">$conn<\/span><span style=\"color: #333333;\">-&gt;<\/span><span style=\"color: #0000cc;\">connect_error<\/span>);\n   }\n\n   <span style=\"color: #996633;\">$sql<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"SELECT * FROM dcsdb.tblformats\"<\/span>;\n   <span style=\"color: #996633;\">$results<\/span> <span style=\"color: #333333;\">=<\/span> mysqli_query(<span style=\"color: #996633;\">$conn<\/span>, <span style=\"color: #996633;\">$sql<\/span>);\n\n   <span style=\"color: #996633;\">$rows<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">array<\/span>();\n   <span style=\"color: #008800; font-weight: bold;\">if<\/span>(mysqli_num_rows(<span style=\"color: #996633;\">$results<\/span>) <span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>){\n\t<span style=\"color: #008800; font-weight: bold;\">while<\/span>(<span style=\"color: #996633;\">$record<\/span> <span style=\"color: #333333;\">=<\/span> mysqli_fetch_assoc(<span style=\"color: #996633;\">$results<\/span>)) {\n\t\t<span style=\"color: #007020;\">array_push<\/span>(<span style=\"color: #996633;\">$rows<\/span>, <span style=\"color: #996633;\">$record<\/span>);\n\t}\t\t\n\t<span style=\"color: #008800; font-weight: bold;\">print<\/span> <span style=\"color: #007020;\">json_encode<\/span>(<span style=\"color: #996633;\">$rows<\/span>);\n   }\n   <span style=\"color: #008800; font-weight: bold;\">else<\/span> {\n\t<span style=\"color: #008800; font-weight: bold;\">echo<\/span>(<span style=\"background-color: #fff0f0;\">\"No records found\"<\/span>);\n   }\n<span style=\"color: #557799;\">?&gt;<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong id=\"t5\">5. Create a Web Application in IIS<\/strong><\/p>\n<p>Before can deploy your API, you need to create a web application in IIS. This is very easy too.<\/p>\n<p>First create a folder inside C:\/inetpub\/wwwroot. Name this folder demoweb(you can also choose another name)<\/p>\n<p>Open IIS,<\/p>\n<p>Right-click on the DefaultWebsite and choose &#8216;Add Application&#8217;<\/p>\n<p>Give it an alias, demoweb. The Add Applicaiton dialog box is shown below<\/p>\n<p>Set the Physical Path to the folder you created.<\/p>\n<figure id=\"attachment_1232\" aria-describedby=\"caption-attachment-1232\" style=\"width: 735px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Defaultwebsite-in-IIS.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1232 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Defaultwebsite-in-IIS-1024x656.jpg\" alt=\"Defaultwebsite in IIS\" width=\"735\" height=\"471\" \/><\/a><figcaption id=\"caption-attachment-1232\" class=\"wp-caption-text\">Adding new Application in IIS<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p><strong id=\"t6\">6. Deploy and Test the API<\/strong><\/p>\n<p>To deploy our API, simply copy the service.php file\u00a0into the directory C:\/inetpub\/wwwroot\/demoweb.<\/p>\n<p>Then visit http:\/\/localhost\/demo\/services.php.<\/p>\n<p>You would see that the data in the MySQL database is returned as json.<\/p>\n<p>If you have any errors, then watch the video and follow the step by step.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t8\">7. Next Steps<\/strong><\/p>\n<p>So we succeeded in creating a REST API that performs a GET operation.<\/p>\n<p>What about POST, PUT and DELETE?<\/p>\n<p>Also, how do we get a single record by the Id.<\/p>\n<p>We would explain this in the next article and in the video as well.<\/p>\n<p>I recommend you subscribe to my channel so you get updates when new lessons are made. Besides, if you have challenges, you can let me know<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, I will teach you how to build a REST web service that would connect to a MySQL database. Then it would fetch &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[289],"tags":[],"class_list":["post-1948","post","type-post","status-publish","format-standard","hentry","category-rest-web-services"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1948"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1948\/revisions"}],"predecessor-version":[{"id":2205,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1948\/revisions\/2205"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}