<?php
class User {
// properties
public $table = 'users';
public $id;
public $username ;
public $password ;
public $first_name ;
public $last_name ;
Above (id, username..., last_name) are the column of users table in MySQL Database. After fetching(I'm using PDO) a specific record from users table using id, how can I assign all records to their respective properties dynamically(a function may help that will search through all available properties and search those properties in the resulting associative array and then assign those specific values to their respective properties)?
// methods
public function set_user($id){
global $dbh;
// sql
$sql = "SELECT * FROM $this->table WHERE id = {$id}";
// return result array
$result_arr = $dbh->pdo_query_fetch($sql);
}
$result_arr contains associative array of id, username, password, first_name and last_name.
NOTE : Here pdo_query_fetch() is a user-defined function.
$dbhyour object would not work any longer. Instead inject the PDO connection when you create the object or use a setter method, that is more save and easier to change later.