PHP REST API CRUD 1: Create API in PHP to fetch data from database in php mysql | Database & Read

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys in this video we are going to learn how to create a rest or a restful API in PHP so guys we are going to create a complete crud application for API in PHP so guys now in this video we are going to start with creating the database connection and fetching the records using API okay so guys to test this we are going to use a software named as Postman done and now is let's get back to our xam server start your Zam server and MySQL and then let's get back to our folder hddocs and we're inside that created one folder PHP API okay so guys let's open this in our vs code editor so whatever editor you use just open in that so first let's create the database connection so first I'm going to create one folder with the name Inc that means like includes and let's create a database con dbcon.php so database connection.php file and now guys in this start PHP and and PHP so and now let's give the host username password and database so tell your host equals to where localhost and then username equal to root and then password will be null and then DB name which is PHP tutorial so already I have created the database guys let me just show you so this is my database name PHP tutorial let me copy that it's a empty database so yep get back paste it and now guys let's give the database connection so create a variable called con that is connection and connect the database using mysqli connect function and then give you a host then username and then password and finally DB name so that is database name as simple guys and now let's check whether the database connection is done or not so you can just check in the condition that is if not database connection then die function you can call here and just tell connection failed okay and just use mysqlite connect error function to display what error you're getting all set guys our database connection is done and now let's begin with fetching the data from our database using the API so now guys let's take an example of customer records okay so where we'll be storing name email and phone number as simple so guys let me create a folder with the name customers okay let me create a file inside customer named as index or read.php read dot PHP file so open PHP and close PHP so first step to create the API in PHP you need to call your headers the basic headers okay so let me just paste and show you done guys let me just show you here also so this is the access control allow origin we are going to allow everything and this is a content type which is going to be Json format so whatever we data send from the back end we are going to send in Json format only and this read.php should be accessible only by the request method get okay not post put delete any other methods it should only allow for the get method okay and then this is your headers part where you can send that content type access Adder authorization and access request with which is for the inputs and all okay so now guys you can begin with other things a request method equal to Dollar underscore server you can just get this request method r e q u e s t underscore method so this will be the request method and here we are going to check if this request method is double equal to get method okay so if it is get then you are going to show else you will tell like let me print that dollar data on message where inside array I will show as status which is 405 that means method not allowed okay and if your message I want to save as a request okay let me copy this request method and you can mention method not allowed okay so like if the request method is not get then you are coming here like post method not allowed put delete any other methods okay and once done you are going to send the header as http forward slash 1.04 not five method not allowed okay we'll explain you how this is going to work and Echo Json underscore and code and code function and the data you are going to send all set so whatever you print here you send it here done and now we are understanding this as a customer record so let's tell customer customer list equal to we will create a function get customer list function okay and we are going to Echo this customer list directly Echo that's it guys and now this function where are you going to use so let me create one file here inside the customer folder that will be function dot PHP are simple now you can get back and include that file function dot PHP file as simple okay so inside this function.php we are going to create that function let me open PHP and close PHP here and now let's get back to read.php here you see get list I mean get customer list let's copy this function name and create that function so function get customer list open and close the function and now guys here we have to fetch that customer data okay so for that guys you need to create the table in your database so let's create a new table so here is a create table that is customers and it's going to be four columns let's go okay so first ID length equal to 11 and here I'm going to select the index as primary go and the auto increment Ai and come with the name worker 191 email wear cap 191 and then phone worker 191 and now guys let's save it so here is the save button let's save perfect so customer table created and here you have few columns okay so that is ID name email and phone and now guys let me just insert two dummy records so let's go on insert option here on the customer table so I'm going to insert with prakash with at gmail .com and phone number okay so second record I'm going to create are om prakash so um prakash gmail.com and then phone number okay and now guys let's go here to insert these records done now if you browse you see that two records will be inserted perfect so now guys let's fetch This Record let's get back so first step we are going to get the database connection so before that you need to require once require where you'll move one step back and then go to ANC folder dbcon.php file and now guys let's call the connection here so you need to make it as global so you can access inside the function okay so this is nothing but your dbcon variable so inside dbcon.php file this is a connection variable which I am accessing over here type query equals to where you will tell select star from customers table okay and then execute the query query underscore run is equal to with the MySQL I query function the first it's going to be the database connection and then your query and then guys you can use a if condition with a record exist or not so first you can check whether this is so if this is true you are going in else you can tell 500 internal server error so for that guys let me just show you how you can enter the message for that let me get back to the read.php here I can copy this the complete code and paste here in the else condition as simple and instead of 405 let's solve 500 and copy and paste here until internal server errors and now guys you can get back to this query okay so once if the query is true you are going to check whether record exist or not so you can tell mysqli number of rows function is greater than zero so whatever query is executed does that have any data or not okay and then use the else part so inside this else condition no record found like the customer details are not found so let me copy this and paste again here and tell for not four let me place here no customer found and the it's going to be four not four and just Echo not Echo instead we have to return it okay our return Json here also you have to return okay so inside the function you are not going to Echo anything Echo is only done on a read.php file all set let's get back so now guys the record is found and greater than 0 then you're coming inside the if condition so here let's get the response so dollar res with the mysqli underscore fetch underscore all function okay inside this all function you are going to get your query executed query run okay your result so this is a query run and we are going to fetch with the sqli associated data format okay so MySQL I and then guys you can show the success message let's copy and paste here yep now you can mention as 200 and here you will mention customer list fetched successfully you can copy let me cap make it capital and here we paste customer list fetch successfully or you can mention that okay also save it so all done guys let's get back to the read.php you will get the data so as simple if we get back to our browser and access the path that is PHP API okay and it goes to customer folder let's move so first localhost localhost PHP API slash customers okay hit enter so here you find read.php let's click here you see customer list fetched successfully you get only the message so we need to get the data also so what we have done let's go to function.php and here you see only you have sent the status so you need to send the data also so what we'll do we'll add the data and send the response let's copy this and send all set guys that's it and now if you get back here refresh you get the status you get the message and you get your data whatever we have added so guys to view this format we have to install your Json formatter plugin okay so in your laptop the data might be seen in this format so this is the Json formatter okay so now guys same format we are going to copy this and try to fetch using the postman so select the get request here and send the URL and now let's send the request you get the response here status 200 and here you get your data all the data so now guys here you see in this status that is 200 and okay it's given so this is coming with the help of this okay so if you want to print as success you can see this also okay let me just resend the request and you will see find that instead of okay it's showing success over there done so this is the status format updating let's get back done so let's keep it as okay only and now guys I just want to check this else part also so for that for how many records is there it's two records right and one and two so let me just change this condition it should be greater than 2 or 3. let's see and now obviously you will get in the else part let's send request and here you see that status 404 message no customer found so here you see that status it is 404 no customer found the message also is updated over here so this is the HTTP headers okay so which is helping here to set that 404 and in the else condition also which is going to set when the internal server error comes okay so now guys you can set this back to zero done so guys in this video we have successfully learned how to create a API to fetch the data okay in PHP MySQL so guys let's continue in next video how we can insert the data using a API which is going to be created in phpmysql so guys will be in this video that's it thank you for watching this video please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 52,555
Rating: undefined out of 5
Keywords: fundaofwebit, how to create rest api in php, how to create api and fetch data from database in php, create api in php to get data from mysql database, rest api in php, php rest api get json data, php laravel rest api tutorial, php curl api tutorial, create an api in php to get data using php mysql, php api to get or retrieve data, php restful api tutorial, rest api tutorial in php, php api tutorial in hindi, php crud for rest api
Id: EDoegvD4YFI
Channel Id: undefined
Length: 17min 4sec (1024 seconds)
Published: Sun Nov 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.