Greetings!
I’m trying to convert this mysql class to sqlsvr pdo:
$found_user = User::authenticate($username, $password);
public static function authenticate($username="", $password="") {
global $database;
$username = $database->escape_value($username);
$password = $database->escape_value($password);
$sql = "SELECT * FROM users ";
$sql .= "WHERE username = '{$username}' ";
$sql .= "AND password = '{$password}' ";
$sql .= "LIMIT 1";
$result_array = self::find_by_sql($sql);
return !empty($result_array) ? array_shift($result_array) : false;
}
public static function find_by_sql($sql="") {
global $database;
$result_set = $database->query($sql);
$object_array = array();
while ($row = $database->fetch_array($result_set)) {
$object_array[] = self::instantiate($row);
}
return $object_array;
}
private static function instantiate($record) {
// Could check that $record exists and is an array
$object = new self;
// Simple, long-form approach:
// $object->id = $record['id'];
// $object->username = $record['username'];
// $object->password = $record['password'];
// $object->first_name = $record['first_name'];
// $object->last_name = $record['last_name'];
// More dynamic, short-form approach:
foreach($record as $attribute=>$value){
if($object->has_attribute($attribute)) {
$object->$attribute = $value;
}
}
return $object;
}
My problem is, is there any equivalent oop pdo classes from this?.I decided to use the class which is I think efficient and flexible. I haven’t tested my code but this is my initial try.
public static function authenticate($username="",$password="")
{
SQLDatabase::conn()
$sql = 'SELECT id,username, password*
FROM ip.tbl_user
WHERE username= :uname
AND password= :pass ';
$result_array = self::find_by_sql($sql);
return !empty($result_array) ? array_shift($result_array) : false;
}
public static function find_by_sql($sql="") {
$st = SQLDatabase::$db->prepare($sql);
$st->bindParam(':uname', $username);
$st->bindParam(':pass', $password);
$st->execute();
$object_array = array();
$row = $st->rowCount();
while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
$object_array[] = self::instantiate($row);
}
return $object_array;
}
private static function instantiate($record) {
// Could check that $record exists and is an array
$object = new self;
// Simple, long-form approach:
// $object->id = $record['id'];
// $object->username = $record['username'];
// $object->password = $record['password'];
// $object->first_name = $record['first_name'];
// $object->last_name = $record['last_name'];
// More dynamic, short-form approach:
foreach($record as $attribute=>$value){
if($object->has_attribute($attribute)) {
$object->$attribute = $value;
}
}
return $object;
}
Any help will do. thank you .