Php RESTAPI结构阻止数据库访问
我正在托管文件hostinger.in,并正在实现REST体系结构。当我在没有休息的情况下实现代码时,我没有得到任何错误。但现在看来我无法访问MySql数据库。我缩小了可能出现的错误,当我尝试注册一个用户时,数据库似乎无法访问。代码如下:Php RESTAPI结构阻止数据库访问,php,android,mysql,sql,rest,Php,Android,Mysql,Sql,Rest,我正在托管文件hostinger.in,并正在实现REST体系结构。当我在没有休息的情况下实现代码时,我没有得到任何错误。但现在看来我无法访问MySql数据库。我缩小了可能出现的错误,当我尝试注册一个用户时,数据库似乎无法访问。代码如下: public function sendOtp($mobile, $model, $brand, $device, $fingerprint, $hardware){ //Checking for User registration if
public function sendOtp($mobile, $model, $brand, $device, $fingerprint, $hardware){
//Checking for User registration
if (!$this->isUserExists($mobile)) {
$otp = rand(100000, 999999);
$stmt = $this->con->prepare("INSERT INTO user_register(mobile, registered_mobile_model, registered_mobile_brand, registered_mobile_device, registered_mobile_fingerprint, registered_mobile_hardware, otp) values(?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssss", $mobile, $model, $brand, $device, $fingerprint, $hardware, $otp);
$result = $stmt->execute();
$stmt->close();
//If statment executed successfully
if ($result) {
//Returning 0, otp sent
return 0;
} else {
//Returning 1, failed to insert
return 1;
}
} else {
//user exists, update info and sent otp
$otp = rand(100000, 999999);
//Crating an statement
$stmt = $this->con->prepare("UPDATE user_register set otp = ?, registered_mobile_model = ?, registered_mobile_brand = ?, registered_mobile_device = ?, registered_mobile_fingerprint = ?, registered_mobile_hardware = ? WHERE mobile = ?");
//Binding the parameters
$stmt->bind_param("sssssss", $otp, $model, $brand, $device, $fingerprint, $hardware, $mobile);
//Executing the statement
$result = $stmt->execute();
//Closing the statement
$stmt->close();
//If statement executed successfully
if ($result) {
//Returning 0, user insert/update success, otp sent
return 0;
} else {
//Returning 1, failed to insert
return 1;
}
}
}
private function isUserExists($mobile) {
$stmt = $this->con->prepare("SELECT id from user_register WHERE mobile = ?");
$stmt->bind_param("s", $mobile);
$stmt->execute();
$stmt->store_result();
$num_rows = $stmt->num_rows;
$stmt->close();
return $num_rows > 0;
}
用于重新路由api调用的index.php代码:
$app->post('/sendotp', function () use ($app) {
//Verifying the required parameters
verifyRequiredParams(array('mobile', 'model', 'brand', 'device', 'fingerprint', 'hardware'));
//Creating a response array
$response = array();
//reading post parameters
$mobile = $app->request->post('mobile');
$model = $app->request->post('model');
$brand = $app->request->post('brand');
$device = $app->request->post('device');
$fingerprint = $app->request->post('fingerprint');
$hardware = $app->request->post('hardware');
//Creating a DbOperation object
$db = new DbOperation();
$res = $db->sendOtp($mobile, $model, $brand, $device, $fingerprint, $hardware);
//If the result returned is 0 means success
if ($res == 0) {
//Making the response error false
$response["error"] = false;
//Adding a success message
$response["message"] = "OTP Sent";
//Displaying response
echoResponse(201, $response);
//If the result returned is 1 means failure
} else if ($res == 1) {
$response["error"] = true;
$response["message"] = "Oops! An error occurred while registering";
echoResponse(200, $response);
}
});
只需确保新的
DbOperation
确实打开了连接。你的代码看起来不错(我的意思是它应该可以工作)。这不是休息与否的问题。REST只是其他系统/人员调用您的API、url组织和操作的方式。不会改变连接到数据库的方式。只要确保DbOperation正常工作。DbOperation正在打开连接。我调查了这个过程,发现请求附带的值为null。我没有使用DbOperation,只是将参数发回以响应检查,发现它们为null。但我似乎无法回答为什么?我尝试使用不同的chrome扩展来发送数据,但似乎都是空的。你在浏览器上使用post方法吗?是的,我使用post方法和内容类型application/x-www-form-unlcoded。事实上,我尝试了每种内容类型,但参数中仍然得到空值