在我的电脑上本地安装了php 7.2。 &安培;关于Godaddy PHP版本是7.4
那是一个带有mysql数据库的post webservice。我们需要从请求体传递id,它与表数据匹配,并以json对象的形式返回匹配行。
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
header('Content-type: application/json');
$response = array();
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
if (!isset($_POST['id'])) {
$response['code'] = 0;
$response['message'] = 'Please set AppID (id) before submit';
echo json_encode($response);
} else {
$id = $_POST['id'];
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
//$sql = "SELECT * FROM appdetails where `AppPackage` = '$id'";
$sql = "SELECT * FROM appdetails where AppPackage = " . $id;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$op = $result->fetch_all(MYSQLI_ASSOC);
$op['error_code'] = 1;
echo json_encode($op);
} else {
$response['code'] = 0;
$response['message'] = 'AppID is not define.';
echo json_encode($response);
}
}
} else {
$response['code'] = 0;
$response['message'] = 'Calling method is wrong.';
echo json_encode($response);
}
?>