Originally I used concatenated queries created by for loops to add large numbers of players/users to a mysql DB. Today, I wanted to experiment with classes because I didn't really understand their use case.
In the class below the new user(+data) is added to the DB and returns the unique ID number for the last inserted row.
I would have assumed that doing this insert as one query would be fastest way of doing it, rather than each time I instantiate a new player, but to be honest I'm still at the beginner phase so would welcome some advice, or suggestions on how best to optimise this.
Ideally as one query, Unless I'm wrong in assuming this is faster?
Is it possible to return the Unique ID's for multiple players as an array?
class player {
var $player;
var $PlayerID;
var $email;
function create_player($new_player, $email, $password, $db) {
$this->player = $new_player;
$this->email = $email;
$salt = "xxx"; //simplified for this example
$passwordhash = $password; //simplified for this example
$query = "INSERT INTO userstest (name, email, hash, salt ) values(:name, :email, :hash, :salt)";
$params = array (':name' => $new_player, ':email' => $email, ':hash' => $passwordhash, ':salt' => $salt);
$stmt = $db->prepare($query);
$stmt->execute($params);
$id = $db->lastInsertId();
$this->PlayerID = $id;
}
}