Hey Everyone-

I am relatively new to PHP and am trying to get a login page to validate a user name and password with a MySQL database.

i have a function.php file that checks the database and either validates the user information or returns an error.

I am stuck on an error that is being returned to me because line 32 seems to have no issues with the syntax and the variables look like they are all defined and being called correctly.

any help would be greatly appreciated. Thanks so much.

line 32 is: $r = @mysqli_query($connection, $q); //run the query

here is the code:

<?php

function check_login ($connection, $user_name='', $user_pass='') {
	$errors = array(); //Initialize the error array

	//validate the e-mail address
	if (empty($user_name)) {
		$errors[] = 'Oops, you forgot to enter a user name.';
	} else {
		$n = mysqli_real_escape_string($connection, trim($user_name));
	}

	//validate the password
	if (empty($user_pass)) {
		$errors[] = 'Opps, you forgot to enter a password.';
	} else {
		$p = mysqli_real_escape_string($connection, trim($pass));
	}

	if (empty($errors)) {
		//if everything checks out ok

		//retrieve the user_id and first_name for that username and password combination

		$q = "SELECT users_id, user_name
			  FROM users
			  WHERE user_name = '$n'
			  AND user_pass = '$p';"

		$r = @mysqli_query($connection, $q); //run the query

		//check the result:
		if (mysqli_num_rows($r) == 1) {
		//fetch the record
		$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);

		//return true and the record
		return array(true, $row);
		} else {
			//not a match!!
			$errors[] = 'Sorry, the user name and/or password you provided do not match those on file.';
		}
	} // end of empty errors IF

	//return false and the errors
	return array(false, $errors);
} //end of check login function

?>

    Look where the semicolon is 😉:

    $q = "SELECT users_id, user_name
    FROM users
    WHERE user_name = '$n'
    AND user_pass = '$p';"
    

      Welcome to PHPBuilder.com! Please use the boards [NOPARSE]

      ...

      [/NOPARSE] tags as they make your code easier to read and analyze. As for the problem, this line:

      $row = mysqli_fetch_array ($r, MYSQLI_ASSOC);
      

      has a space between the function name and the paren. Remove it and you should be golden. And on this line:

      AND user_pass = '$p';"

      The semicolon needs to be AFTER the double quotes.

      Edit: Darn you bonesnap!

        Derokorian;10999077 wrote:

        has a space between the function name and the paren.

        The PHP parser doesn't care about whitespace; foo() is the same as 'foo ( )' or 'foo

        (
        )'.

          Oh - I always thought that was true except I thought functions had to be tight. I guess that's just more of a coding standard than requirement then.

          The other point about the semicolon still holds true though.

            sorry sorry for the dumb question.

            everything starts to blur after learning, writing and checking for errors for a few hours.

            thanks so much everyone

              mshane8;10999084 wrote:

              everything starts to blur after learning, writing and checking for errors for a few hours.

              Yes yes it does. Trust me sometimes it can be a pain because you look at something so many times you don't even notice its wrong. That's why sometimes you just need another pair of eyes to look at something. You should stay active on these boards, I've found the people here to be amazingly insightful =D

                Write a Reply...