PHP REST API CRUD 3: Create API to fetch single record from MySql DB | fetch data using API in PHP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys we are continuing our rest API in PHP so guys we have already learned how to create an API to store the data and also how to create an API to fetch the data okay from our mySQL database so now guys in this video we are going to learn how to fetch other single record that means we are going to fetch one row from the mySQL database so let me just explain you in the database guys let us go over there in our database customer table so here we have four records so I just want to fetch each records one by one okay so now guys let's get back to our vs code so first step guys we are going to our read.php file so let me just zoom it okay so here we have given the condition as request method get and it gets the customer list that is visible over here let me just show you this output so this is my read.php file where you have got this output okay so now guys let's fetch each records so let's get back and here we are going to add one if condition dollar underscore get method ID and then use a else condition here and just paste this get customer list function inside the else part as simple as it's more clear and now guys before checking this I want to check in the assert function whether it is set or not that ID okay when it is set that time we are going to get the customer okay dollar customer equal to get customer function and Echo that customer Echo dollar customer done and now guys we need to pass this get method and set this so we can get in this function so now guys let's copy this complete function name and go to our function.php file so as simple guys let's move to function.php and just create a simple function so let's move below here and let me create the function function with the function name get customer open and close calibrous and here you are getting the ID so that is nothing but customer parents we are sending so let's tell customer params okay that is parameters we are getting from there are simple and now guys let's define the connection here give the connection Global and let me just copy from above for the validation copy this one if condition paste enter the customer ID if the customer ID is not provided let's copy this let me remove everything and just pass the dollar customer parameters whatever in that it comes that is ID is double equal to null okay so when it is null you are going to return that error 422 that is enter your customer ID 422 function let me just show you so here I have created it okay so let's get back to the get customer function here and now guys let's write the code so first let us get this customer ID so customer ID equal to Let's validate the customer ID masculine real Escape string function pass the connection and then the customer ID so let us get the ID paste here and now guys let's type the query where you will tell select star from the customer table customers where ID equal to inside single quotes and you pass the limit which I should get one and the customer ID is here we get this and pass Here and Now guys let's execute this query query or let's tell a result what result we get from the above query mysqlite query function so we are using this function to execute the above query so let's copy this so before pasting this you need to pass the database connection and then your query okay so query is successful so now guys let us check this in if else condition if this query is true like there is no mistake or database connection at that time you can use this else here in the else part I just want to give the message let me copy from above and paste here so here we are going to tell 500 that is internal server error same data and just return the Json encode data so guys once the query is successful we are going to check whether it has a record or not so first let us use a MySQL I number of rows function is greater than 0 or equal to 1 I just want one record so I just wanted to check only one record in this result if it is present then we are going to get the response okay whatever data comes else what we have to do we need to say that that no customer found so for that guys you can just copy the below code and paste here and make this stuff for not four and here let's tell no customer font are simple and here you have to give four not four and not found are simple so guys once the data is found if it is more than one record then you are going to get the response from the MySQL I fetch Associated data okay so where you will pass this result of variable and get that data so then guys you can again copy this copy it and paste here set the status which is 200 and here customer fetched successfully and then here also which is going to be 200 and send it success or OK any data okay done guys and now you need to pass one more every data which is inside the data you're going to pass your result result response okay done save it and now guys let's get back to the read.php file and see that ID parameter let's copy this and get back to the Google Chrome let's go to that read.php file I'm enter this file whatever we are working on okay and just send the parameter ID okay equal to null I'm going to send first here so here you see guys that enter your customer ID you have got the message let me Zoom it so here you get the customer enter your customer ID then only you'll get so now guys I am going to enter id1 so here you get your data that is with prakash so let me just Fetch and see in the next tab on the read.php I am not passing any parameter so here you see guys we have total four records so let me fetch the ID number four so here you get ID number four data and now guys let me check with wrong okay 55 ID which is no customer found okay so we get all the validation done if you just remove this parameter here you see if you remove all the read.php file will work okay all the customer list will fetched and if you try to import any other ID okay so right now you are putting ID right so if I change this customer ID equal to one so what happens in this case it's not going to accept because it's just checking for the ID get ID so over here you can see that if it is set to ID only then only it will come inside else it's going to return only this else part okay so now guys same condition Let's test with the postman also so let's get back to our Postman and here let's pass it and it's on the read.php right now so let's just send the request what we get the response we get all the customer list data so here you can see all the customer list we have fetched perfect so now guys I just want to fetch one single record so you can add a parameter ID equal to one either you type here in this URL itself or else you can just type here in the parents so you just click on this patterns and get back ID and values 1 it will automatically type and paste off okay and now guys let's send the request and make sure you are selecting the get method it's not going to be a post method okay so here you see you have fetch the ID number one detail if you try to post with the post method okay send the request with post method then it will tell that method not allowed post method not allowed so guys let me just give some space over here because it's getting messed post method here in this else condition in the request check let's save it now we can get back and let's resend the request so here you see post method not allowed let's make back to again get method send the request and you will find perfect so customer fetched successfully and here is your data let me change the value to 3. and send the request again quote so guys we have successfully done with how to create an API to fetch the single record in PHP okay so guys in this video that's it thank you for watching this video guys please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 7,970
Rating: undefined out of 5
Keywords: fundaofwebit, create an api in php fetch data from database, php api to fetch single record in php mysql, php mysql get data from mysql database in php mysql, how to create api in php mysql, Create API to fetch single record from MySql DB, fetch single record from the rest api in php, php rest api tutorial, php rest api crud, php api using mysql database, PHP RESTFUL API tutorial for beginners, php api tutorial, store data using api in php mysql
Id: zVoFXxgfWlU
Channel Id: undefined
Length: 11min 0sec (660 seconds)
Published: Sat Dec 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.