Skip to content

Instantly share code, notes, and snippets.

@Slaver
Created July 3, 2024 21:36
Show Gist options
  • Save Slaver/36db3eb0ed9a3aa3697cac8bf63506c9 to your computer and use it in GitHub Desktop.
Save Slaver/36db3eb0ed9a3aa3697cac8bf63506c9 to your computer and use it in GitHub Desktop.
<?php
class UserManager
{
private $db;
public function __construct($db)
{
$this->db = $db;
}
public function createUser($username, $email, $password)
{
$query = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')";
$result = mysql_query($query);
if ($result) {
return true;
} else {
return false;
}
}
public function getUserByEmail($email)
{
$query = "SELECT * FROM users WHERE email = '$email'";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
return mysql_fetch_assoc($result);
} else {
return null;
}
}
public function updateUserPassword($userId, $newPassword)
{
$query = "UPDATE users SET password = '$newPassword' WHERE id = $userId";
return mysql_query($query);
}
}
// Run
$userManager = new UserManager($db);
$user = $userManager->getUserByEmail('example@email.com');
if ($user) {
echo "User found: " . $user['username'];
} else {
echo "User not found";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment