Could not fetch array because:

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Could not fetch array because:

Post by cturner »

I have the error that is in the title when I test the following code with a username that isn't in the database. Can someone please tell me why and how I can fix it? Thanks in advance.

Code: Select all

require "config2.php";
$arrErrors = array();
// checking to see if the submit button has been pressed
if (isset($_POST['Submit'])) {
// checking to see if the username field is blank
	if ($_POST ['username'] == '') {
	// if username field is blank display an error
		$arrErrors['username'] = 'Please enter your username.';
	}
// if no errors send the password
if (count($arrErrors) == 0) {
// get the username from the form
$username = mysql_real_escape_string($_POST['username']);
// select the username, password and email address
$query = mysql_query("SELECT `username`, `password`, `email` FROM `users` WHERE `username`='$username'");
$check = mysql_num_rows($query);
$rows = mysql_fetch_array($query) or die ("Could not fetch array because: ".mysql_error());
if($check != 0) {
	$password=$rows['password'];
	$to=$rows['email'];
	$subject="Your Barwonnp.com.au Password Retrieval";
	$header="From: Camille Turner <camille.turner@bigpond.com>";
	$messages="Dear $username \r\n";
	$messages.="You are receiving this e-mail because you requested a password reminder from barwonnp.com.au. \r\n";
	$messages.="As requested, please find below your password. \r\n";
	$messages.="Your password: $password \r\n";
	$messages.="Regards \r\n";
	$messages.="Kevin Humphries \r\n";
	$messages.="www.barwonnp.com.au \r\n";
	$messages.="Please ensure that this password is not accessible by others, to ensure security of your information. \r\n";
	$sentmail = mail($to,$subject,$messages,$header);
} else {
	echo "Your username isn't in our database.";
}
if($sentmail){
	header ('Location: password_sent.php');
} else {
	echo "Cannot send password to your e-mail address.";
}
    } else {
        // The error array had something in it. There was an error.
        // Start adding error text to an error string.
        $strError = '<div class="formerror"><p>Please check the following and try again:</p><ul>';
        // Get each error and add it to the error string
        // as a list item.
        foreach ($arrErrors as $error) {
            $strError .= "<li>$error</li>";
        }
        $strError .= '</ul></div>';
    }
}
mysql_close();
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is there anything else after that "error"?

Why do you have an "or die()" on the fetch line anyway? If no records were found the die() will run.
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

My script is now working thanks to feyd.
Post Reply