我想以编程方式将参数传递给php配置文件。
DbOperations.php中的代码:
function getName() {
$stmt = $this->con->prepare("SELECT name,teamaffiliation
as team FROM heroes WHERE id = 6");
$stmt->execute();
$stmt->bind_result($name, $team);
$heroes = array();
while($stmt->fetch()) {
$hero = array();
$hero['name'] = $name;
$hero['team'] = $team;
array_push($heroes, $hero);
}
return $heroes;
}
这是我的api.php:
case 'getname':
$db = new DbOperation();
$response['error'] = false;
$response['message'] = 'Request successfully completed';
$response['heroes'] = $db->getname();
break;
我想以编程方式传递ID(id = x
)的值,请帮助我实现这一目标。
此外,我需要将参数从android传递到php。我尝试了以下代码,但这也不起作用。
private void retrive() {
HashMap<String, String> params = new HashMap<>();
params.put("id", "5");
PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_READ_NAME, params, CODE_POST_REQUEST);
request.execute();
}
答案 0 :(得分:0)
对于RESTful API,您可以通过GET传递ID,因此您的URL看起来像/api.php?id=1
。
然后在api.php
中,您可以使用$_GET['id'];
访问该变量,并将其传递到getname
函数中。
例如。
case 'getname':
$db = new DbOperation();
$response['error'] = false;
$response['message'] = 'Request successfully completed';
$response['heroes'] = $db->getname($_GET['id']);
break;
然后,您可以在函数中将id = 6
更改为id = ?
,并使用?
将参数($id
)绑定到变量$stmt->bind_param("i", $id);
上{{1 }}表示参数是整数,其他选项是:
例如:
i