doushang4293 2014-11-11 10:54
浏览 44
已采纳

如何在PHP中为Android应用程序后端编写REST Web服务?

Currently, I am developing an Android application with involves client server architecture. It is said to me that I have to write REST web service in PHP for back end communication. At that time I didn't know about RESTful architecture and etc..

In last 3 days, I learned much about REST web services and tried many tutorials. Then, I tried some code from tutorials and from SO. What I have tried so far is as follows:

I have three php files, a database named xyz and table named user_accounts with basic user details in phpmyadmin. And I have installed Advanced REST client on my browser. All code is in WWW directory of WAMP server under folder named my project. So, let me show some code:

1. db_connect.php

 <?php

            define("SERVER", '127.0.0.1');
            define("USER", 'root');
            define("PASSWORD", '');
            define("DB", 'xyz');


            $con = new mysqli(SERVER,USER,PASSWORD,DB);

            if ($con->connect_errno){
                die("Database Connection Failed");
                exit();

            }

In second file I have a function named adduser for adding user records into the database :
index.php :

    <?php
            require_once('db_connect.php');

           $response = array();

           $result = "";


           function adduser($firstname, $lastname, $email, $password) {


            global $app;

            $req = $app->request();

            $firstname= $req->params['firstname'];

            $lastname= $req->params['lastname'];

            $email = $req->params['email'];

             $password = $req->params['password'];


    $stmt = $con->prepare("INSERT INTO user_accounts (first_name,last_name,email,password)VALUES (?,?,?,?)");

                        $stmt->bind_param('ssss', $firstname, $lastname, $email, $password); 

                        $stmt->execute();

                        $result = $stmt->close();


        }

                if($result){

                          $response["success"] = 1;

                          $response["message"] = "account successfully created.";

                          echo json_encode($response);

                }

                else{

                     $response["success"] = 0;

                     $response["message"] = "An error occurred during registration.";

                     echo json_encode($response);
                 }



            ?>    

When I test it using advanced REST client by giving url :

http://127.0.0.1/my project/index.php/adduser

and method POST and parameters:

firstname=somename&lastname=name&email=a@b.gmail.com&password=101010

It shows following response:

{"success":0,"message":"An error occurred during registration."}

I can not identify where the error is. I am new to this. Please help if anything wrong.

  • 写回答

1条回答 默认 最新

  • duancan7914 2014-11-11 11:06
    关注

    your should try this:

    $affected_rows = $stmt->rowCount();
    

    Update:

    then check if row count is greater than 0.

    <?php
            require_once('db_connect.php');
    
           $response = array();
    
           $result = "";
    
    
    function adduser($firstname, $lastname, $email, $password) {
        global $app;
        $req = $app->request();
        $firstname= $req->params['firstname'];
        $lastname= $req->params['lastname'];
        $email = $req->params['email'];
        $password = $req->params['password'];
    
        $stmt = $con->prepare("INSERT INTO user_accounts (first_name,last_name,email,password)VALUES (?,?,?,?)");
    
        $stmt->bind_param('ssss', $firstname, $lastname, $email, $password); 
        $stmt->execute();
    
        return $stmt->rowCount();
    }
    
    $adduser = addUser($firstname, $lastname, $email, $password);
    
    if($adduser > 0){
        $response["success"] = 1;
        $response["message"] = "account successfully created.";
    
        echo json_encode($response);
    } else{
        $response["success"] = 0;
        $response["message"] = "An error occurred during registration.";
    
        echo json_encode($response);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀