Build your first web service with PHP, JSON and MySql

  • Emmanuel Alcime

    some those code I just saw use to much code in order to do something simple but, I like how they use the an array to json encode the data from MySQL.

  • Mahadev Prasad

    This may help some of the people. I got this error and solved. Refer file insert.php; while inserting the values to database, the first value ID is referred as NULL in the insert php. Have removed both the ID key and NULL value from the insert statement. And at phpmyadmin, I have changed the the ID field as a primary key and made it as Auto Increment. The problem is solved.

  • Pandi Esh

    how to retrieve the json data into mysql and mysql into the json data..please sent to the sample code for php…

  • mukesh saini

    Really helpful post, Can you please write a post for SOAP web service.

    • Hi mukesh, i am soon going to publish post about soap service stay tune. 🙂

  • Pingback: Create web service in php mysql |()

  • Bisma Saeed

    Thank u so much for this tutorial. Great help for freshers.

  • nilesh

    hi, sir i want to fetch the data from .json file. i have tried but i dont understand how to.

  • Segun Aruleba Emmanuel

    This is a very good tutorial for a beginner in php programmer and i tell you thank you for all your effort,it is very helpful. I want to really know how to fetch the data from json and xml file. I have been trying to but have not gotten it.

  • Anand Jain

    Helpful post, covered all basics.
    Best for beginner…!
    How can we check uploading of files in it ???

  • Noman Khan

    Can any one tell me what is aneh and rest in the url…. I am using notepad for this ….. no dreamweaver and no any IDE

    • Hi Norman, aneh is name can you plz tell me the issue ur facing .

      • Noman Khan

        I have these files in Desktop so what url should I be using???
        my error is unexpected token and 404 status

  • சங்கர் வெங்கடேசன்

    i am get error Unexpected token < in json

    In Raw i getting

    Notice: Undefined index: name in C:xampphtdocswebappinsert_user.php on line 10

    Notice: Undefined index: address in C:xampphtdocswebappinsert_user.php on line 11

    Notice: Undefined index: mobile in C:xampphtdocswebappinsert_user.php on line 12

    Notice: Undefined index: email in C:xampphtdocswebappinsert_user.php on line 13

    Notice: Undefined index: status in C:xampphtdocswebappinsert_user.php on line 14

    Notice: Undefined index: datetime in C:xampphtdocswebappinsert_user.php on line 15
    {“status”:1,”msg”:”Done User added!”}

    Empty rows inserted in tables

    • Hi, can you plz tell me how you r sending your data to server, so i can help you. Plz confirm that you select request method post before send data to server.

      • சங்கர் வெங்கடேசன்

        Thank you replay,,

        I copy and paste the above coding and run there is no error but empty value is insert in database

  • Mangesh Anpat

    hello everyone ,plz help me out from this error unexpected token < but the value is inserted in database but can't able to show the sing-up page…here is the code.

    1, “msg” => “Done User added!”);


    $json = array(“status” => 0, “msg” => “Error adding user!”);



    $json = array(“status” => 0, “msg” => “Request method not accepted”);



    /* Output header */

    header(‘Content-type: application/json’);

    echo json_encode($json);



    Thanks in advance..

    • Divyesh Vasoya

      you can view error in Row tab.

  • Ashok

    just add @ in mysql_connect
    like @mysql_connect

  • Vatsh Patel

    hello there
    can you please show me how can we delete the data in database

  • Abhshek Gupta

    hello there

    the json is not return in my advance rest client ..

    it only shows Unexpected token < ??

    please resolve my problem

    • Hi abhsek, plz check in php code there is syntax error in it

      • Abhshek Gupta

        but i just copy your all code as you say .. than what is the syntax error ..i am unable to see the json response . data was instered well but not show the success message ..

  • Vinayak

    reason for error was mysql tags are now deprecated

    so if you use mysqli instead of mysql in the code make sure you pass conn object to all statements at line 8,9,10,11 that is where we are using real escape string.


    worked for me.

    also make sure every tag in mysqli having number of parameters to trace for.

    keep alive.!

  • Banu Priya

    Its really superb. Thanks for the coding. I got output like yours.

  • venkatesh p

    really great tutorial thank you so much …

  • Hans Stolte

    The Advanced rest client has changed , so the info you give needs to be changed

    • Hi Hans, thanks I am going to update this post soon 🙂

  • Sankar

    How to set authentication for this rest API url – malwares can inject easily via this url

    • Jared J-rad Meyers

      Exactly. He states early in the tutorial that you MUST add your own authentication. I would suggest using whatever your organization is using for authentication. If you are doing this as a hobby project, then I suggest researching simple php authentication.

  • Vivek Joshi

    I downloaded Advanced rest client. but in this how can i pass a parameter for post request. as you show in above picture. there is no option like that.

    • Jared J-rad Meyers

      So I got this working on the new format. To get the Post request working you must click the drop down menu “Custom Content Type”. This will show the option “application/x-www-form-urlencoded” Select it and change the headers according to the tutorial.

  • Jared J-rad Meyers

    Hello everyone! If you are doing this tutorial now (5/17/2016) or later, then you must do things a bit differently due to changes in php and the Advanced Rest Client. I got everything working and did these 2 minor changes.

    1. PHP deprecated mysql commands and now uses mysqli. The only real difference is that mysqli use the $conn variable to do most commands. So if you are getting errors like “mysqli_real_escape_string() expects exactly 2 parameters, 1 given” then add the connection into the method like this: mysqli_real_escape_string($conn, …

    2. The Advanced Rest Client has changed its format. To get the correct Header information for the POST request. You must click “Custom Content Type” and select “application/x-www-form-urlencoded” This will change how you are sending data in the Web Service. It will embed the data into the header and the code in PHP will receive it. Once that is selected add the headers name, email, pwd, and status with the correct info under the headers form.

    If you have any questions, please let me know! I usually respond every couple days. Good luck fellow coders!

    • ganesh harige

      Hi Jared J-rad Meyers,

      I tried the above steps but not getting the output.

      When i try to access the link
      http://localhost/logintask/webservice1/signup.php in browser i get the below msg.

      {“status”:0,”msg”:”Request method not accepted”}

  • ezenwa jannifer

    thanks alot, it is very helpful

  • Hitesh Danidhariya

    Hi Aneh,Can you post a article on saving the Image in webservice.Its so important for the beginers now!

  • Karishma Vaidh

    I am doing work on “put” method it is not work please help me how can use put in webservices.

  • Jason Duane


  • Thanks.

  • kirankumar

    Thanks! Really nice tutorial to learn.

Trinity tuts is one of the best place for beginners to learn android, php, google and web design tutorial and tips. - Maintained by Aneh Thakur
Subscribe to Blog via Email

Join 3,874 other subscribers