I'm working through "PHP and MySQL Web Development" by Welling and Thomson. The password w/db lookup example provided on the CD is not working. Is the code incorrect or is communication to the MySQL database not working properly?
<?php
}
else
{
// connect to mysql
$mysql = mysql_connect( 'localhost', 'webauth', 'webauth' );
if(!$mysql)
{
echo 'Cannot connect to database.';
exit;
}
// select the appropriate database
$mysql = mysql_select_db( 'auth' );
if(!$mysql)
{
echo 'Cannot select database.';
exit;
}
// query the database to see if there is a record which matches
$query = "select count(*) from auth where
name = '$name' and
pass = '$password'";
$result = mysql_query($query);
if(!$result)
{
echo 'Cannot run query.';
exit;
}
$count = mysql_result( $result, 0, 0 );
if ( $count > 0 )
{
// visitor's name and password combination are correct
echo '<h1>Here it is!</h1>';
echo 'I bet you are glad you can see this secret page.';
}
else
{
// visitor's name and password combination are not correct
echo '<h1>Go Away!</h1>';
echo 'You are not authorized to view this resource.';
}
}
?>
Even with a correct name and password combination I get the "You are not authorized . . ." I have done an echo for $count and the value is 0. I have used phpMyAdmin to perform a query and can indeed match a name and password and select a record.