mysql - How to write REST web service in PHP for Android application backend? -
currently, developing android application involves client server architecture. said me have write rest web service in php end communication. @ time didn't know restful architecture , etc..
in last 3 days, learned rest
web services , tried many tutorials. then, tried code tutorials , so. have tried far follows:
i have 3 php files, database named xyz
, table named user_accounts
basic user details in phpmyadmin. , have installed advanced rest client on browser. code in www
directory of wamp server
under folder named my project
. so, let me show 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 have function named adduser
adding user records 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 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 created."; echo json_encode($response); } else{ $response["success"] = 0; $response["message"] = "an error occurred during registration."; echo json_encode($response); } ?>
when test using advanced rest client giving url :
http://127.0.0.1/my project/index.php/adduser
and method post
, 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 error is. new this. please if wrong.
your should try this:
$affected_rows = $stmt->rowcount();
update:
then check if row count greater 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 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 created."; echo json_encode($response); } else{ $response["success"] = 0; $response["message"] = "an error occurred during registration."; echo json_encode($response); }
Comments
Post a Comment