php - Angular 4 - 如何使用参数将数据获取到数据库

标签 php angular

我想知道如何使用http服务将参数从url传递到php。

服务:

  getEmployeesById(data) {
     let datas = JSON.stringify({'id':data});
      return this.http.get('http://localhost/employees/?p=editEmployees', datas)
    .map(response => response.json() );
  }

组件:

ngOnInit() {
this.activatedRoute.queryParams.subscribe((queryParams: Params) => {
  let userId = queryParams['id'];
  this.employeesServices.getEmployeesById(userId)
    .subscribe(data => {
      this.result = data
    });
});

}

PHP:http://localhost/employees/?p=editEmployees

$editEmployees = mysqli_query($con, "SELECT * FROM $employeesTable WHERE emp_id = '".$_GET['id']."' ");
$rows = mysqli_fetch_assoc($editEmployees);
$emp[] = $rows;
echo json_encode($emp);

-我创建了一个获取员工 ID 的 mysqli 查询。 -然后在组件中,我从 url param(id) 获取 emp id,如下所示

this.employeesServices.getEmployeesById(userId);

我的问题是,我不知道我传递的数据是否正确。

我使用

在 html 中显示数据
{{ result.first_name }}

但我无法正确理解。

最佳答案

您可以在查询字符串中发送它,就像您已经将 p=editEmployees 作为查询字符串中的参数发送一样。您可以简单地在查询字符串中附加另一个参数,如下所示

getEmployeesById(data) { // as data is userId so simply append it 
      return this.http.get('http://localhost/employees/?p=editEmployees&id='+ data)
    .map(response => response.json() );
  }

关于php - Angular 4 - 如何使用参数将数据获取到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46067487/

相关文章:

使用 CanActivate 的 AngularFire2 (4.0) 身份验证不起作用

javascript - 将 Google 可视化图表中所有缺失数据点初始化为 0

php - 试图打开 php 文件的表单

php - 如何从外部服务器上的文件推送 header 进行下载?

html - 来自组件的 Angular 4 模式验证

Angular2 从 HTML 模板访问全局变量

angular - 'size' 、 'prototype' 的所有声明必须具有相同的修饰符

angular - 如何在 Angular 5 View 中访问枚举值

php - Laravel 5 - Paypal 支付 - 将数据表单传递给 Controller

php - Wordpress - 删除动态生成的样式表?