Im trying to write a login script that needs a user to to login before they can access the database and when i write out the code i get the login in screen but the screen gives this error: PHP Fatal error: Call to a member function fetch() on boolean in G:\xampp\htdocs\music\auth.php on line 12
I dont know what to do. Heres my code:
<?php
session_start();
$err = "";
if( !empty($_POST['submit']) )
{
$name = $_POST['user'];
$pwd = $_POST['password'];
$music = new PDO('mysql:host=localhost;dbname=music', 'musicphp', 'password');
$sql = "SELECT id FROM music WHERE username = '$name' AND pwd='$pwd'";
$users= $music->query($sql);
$music = $users->fetch();
if( empty($users['id']) || $users == NULL )
{
$err = "Bad username or password";
}
else
{
$_SESSION['userid'] = $user['id'];
header('Location: index.php');
}
}
echo $err;
?>
<form action = "auth.php" method="post">
Username: <input type="text" name="user"/>
Password: <input type="password" name="password"/>
<input type="submit" name="submit" value="Log in" />
</form>