像返回类型函数一样动态查询

时间:2019-11-02 06:18:48

标签: php jquery database

像返回类型函数那样动态查询我们是否可以在我的编程区域中使用像动态返回类型函数那样的动态函数

```import { Injectable } from '@angular/core';
import { User } from './user.model';
import{ HttpClient, HttpHeaders } from '@angular/common/http';
import{ environment } from '../../environments/environment';
import { from } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class UserService {
  selectedUser: User = {
    fullname:'',
    email:'',
    university:'',
    College:'',
    Department:'',
    password:'',
    admintype:''
  }

  noAuthHeader = { headers: new HttpHeaders({ 'NoAuth': 'True'})};

  constructor(private http: HttpClient) { }


  //http methods
  postUser(user:User)
  {
    return this.http.post(environment.apiBaseUrl+ '/register' ,user)
  }

  login(authCredentials)
  {
    return this.http.post(environment.apiBaseUrl+ '/authenticate',authCredentials,this.noAuthHeader);
  }

  getUserProfile()
  {
    return this.http.get(environment.apiBaseUrl + '/userProfile');
  }


  //helper methods
  setToken(token:string)
  {
    localStorage.setItem('token',token);
  }

  getToken()
  {
    localStorage.getItem('token');
  }

  deleteToken()
  {
    localStorage.removeItem('token');
  }

  getUserPayload(){
    var token = localStorage.getItem('token');
    if(token)
    {
      var userPayload = atob(token.split('.')[1]);
      return JSON.parse(userPayload);
    }
    else
    return null;
  }

  isloggedIn()
  {
    var userPayload = this.getUserPayload();
    if (userPayload)
    {
      return userPayload.exp > Date.now() / 1000;
    }
  }


  getUserRole()
  {

  }

}```

2 个答案:

答案 0 :(得分:4)

您可以这样做

function insert_data($table,$data){
    $sql ="INSERT INTO ".$table;
    $sql .=" (".implode(",",array_keys($data)).") VALUES";
    $sql .=" ('".implode("','", array_values($data))."')";
    $query=$conn->query($sql);
    if ($query) {
        return true;
    }
    else{
        return false;
    }
    $query->close();
}

调用上层函数以将数据插入表中。希望这能解决您的问题

$data=array('name' => "Yasir", 'age' => 25, 'education' => "Master");
$fet=insert_data("student",$data);

答案 1 :(得分:0)

您想要的删除方法是:

   function insert_data($table,$columnName,$matchingWord){
       if($columnName == null && $matching == null || $columnName == '' && $matching == ''){
          $sql= "DELETE FROM".$columnName." WHERE 1";
       }
       else{
          $sql = "DELETE FROM".$columnName."WHERE".$columnName."=".$matchingWord."";
       }
       $query=$conn->query($sql);
       if ($query) {
            return true;
       }
       else{
            return false;
       }
        $query->close();
    }

像这样调用上层函数。

  insert_data("users","id","3");

它将删除ID等于3的用户。
而且,如果您不提供$columnName$matchinWord,它将自动截断该表。