I’m running the following code, but everything it gives me the following errors. I’ve read several articles on SO, still no use.
The error I’m getting is:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/xyz/public_html/13/beta/signup.php on line 49
Warning: Cannot modify header information - headers already sent by (output started at /home/xyz/public_html/13/beta/signup.php:49) in /home/xyz/public_html/13/beta/signup.php on line 69
My PHP script:(I’ve marked the lines 49 & 69)
if(isset($_POST['submit'])&&$_POST['submit']=='Login')
{
$err = array();
if(!$_POST['username'] || !$_POST['pass'])
$err[] = 'All the fields must be filled in!';
if(!count($err))
{
$_POST['username'] = mysql_real_escape_string($_POST['username']);
$_POST['pass'] = mysql_real_escape_string($_POST['pass']);
$_POST['remember'] = (int)$_POST['remember'];
$row = mysql_fetch_assoc(mysql_query("SELECT id,user,email,clg FROM users WHERE user='{$_POST['username']}' AND pass='".md5($_POST['pass'])."'"));-->line 49
if($row['user'])
{
$_SESSION['user']=$row['user'];
$_SESSION['id'] = $row['id'];
$_SESSION['clg'] = $row['clg'];
$_SESSION['email'] = $row['email'];
$_SESSION['remember'] = $_POST['remember'];
setcookie('tcookie',$_POST['remember']);
header("Location: index.php");
exit;
}
else $err[]='Wrong username and/or password!';
}
if($err)
$_SESSION['msg']['login-err'] = implode('<br />',$err);
header("Location: signup.php");-->line 69
exit;
}
Your
mysql_queryfunction is returning an error, instead of a result. Try:This will help you get the error information.
Once that error is gone, you’ll probably find that the ‘headers already send error’ disappears too, it’s a consequence of something already being printed to the output (possibly an error message).
Update: For your ‘No database selected’ error, you need to use
mysql_select_db($databasename);before you usemysql_query(). Otherwise, mysql has no idea which database you’re trying to query.Also, you may want to look into using PDO, it’s another way of accessing MySQL databases, and it’s heaps more secure and a bit easier to use (IMO).