dsqnonh2763
dsqnonh2763
2017-09-06 05:33
浏览 15
已采纳

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

I would like to know how to pass parameter from url to php using http service.

service:

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

component:

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);

-I created a mysqli query that gets the employee id. -Then in component, I fetched the emp id from url param(id) like this

this.employeesServices.getEmployeesById(userId);

My problem is, I dont know If I am passing the data correct.

I display the data in html using

{{ result.first_name }}

but I cant get it right.

图片转代码服务由CSDN问答提供 功能建议

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

服务:

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

组件:

  ngOnInit(){
this.activatedRoute.queryParams.subscribe((queryParams  :Params)=> {
让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); 
 $ \ temp [] = $ rows; 
echo json_encode($ emp); 
   
 
 

-I created 获取员工ID的mysqli查询。 然后在组件中,我从url param(id)中获取了emp id,就像这样

  this.employeesServices.getEmployeesById(userId  ); 
   
 
 

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

我显示数据 html使用

  {{result.first_name}} 
   
 
 

但我无法正确使用。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpdfh60088
    dpdfh60088 2017-09-06 06:06
    已采纳

    you can send it in the query string like you already sent p=editEmployees as parameter in the query string. You can simply append another parameter in the query string like this

    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() );
      }
    
    点赞 评论

相关推荐