PHP - Undefined variable

后端 未结 9 823
星月不相逢
星月不相逢 2021-01-26 05:29

I\'m doing some exercises from Beginners PHP & MySQL by Mr. Tucker.

On his example everything works fine, but on my PC there is an error:

9条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-26 05:52

    You are trying to access the variable $passwordRetrieved when it has not yet been given a value. The access is done here:

    echo "passwordRetrieved = ".$passwordRetrieved."
    ";

    The variable would be set just above:

    while ($row = mysql_fetch_array($tUser_SQLselect_Query, MYSQL_ASSOC)) {
        $passwordRetrieved = $row['password'];  // VARIABLE SET HERE
    }
    

    The important thing is that the variable only gets set if the query returns a matching row (i.e., on a successful login). If the login is not valid, the variable is not set.

    To check if a variable is set without getting a notice, you would use either isset or empty (there are subtle differences, but as a rule of thumb you can use either most of the time). Your code actually already does this just below:

    // Checking if $passwordRetrieved has been set using empty()
    if (!empty($passwordRetrieved) AND ($password == $passwordRetrieved)) {
    

提交回复
热议问题