while循环没有返回结果PHP / MySQL

时间:2013-10-29 00:29:34

标签: php mysql

昨晚它工作正常现在我得到undefined errors来自db的所有变量。如果我在while循环之后回显变量,那么它们就在那里但是一旦它们点击if我就会得到未定义的错误。

   <?php 
   session_start();
   include "connect.php";
   include "report_errors.php";

    $name = $_POST['name'];
    $password = $_POST['password'];


    $sql = $db->prepare("SELECT * FROM admin where username = :name AND 
            password = :password");

    $sql->bindValue(':name', $name);
    $sql->bindValue(':password', $password);

    $sql->execute();

   while($row = $sql->fetch())
   {
   $dbname = $row['username'];
   $dbpass = $row['password'];
   $dbemail = $row['email'];
   }

       if($name == $dbname && $password == $dbpass)
       {
         $_SESSION['loggedin'] = "yes";
         $_SESSION['name'] = $name;
         $_SESSION['pass'] = $password;
         $_SESSION['email'] = $dbemail;

         $url = "Location: yackimo_register_form.php";
         header($url);
         exit;
     }
         if($name == $dbname && $password != $dbpass)
           {
         $error = "invalidPassword";
         $url = "Location: login.php?error=$error";
         header($url);
         exit;
            }

1 个答案:

答案 0 :(得分:0)

您遇到哪些变量错误?好像你的问题可能是因为你没有用SQL返回任何结果 - 因此没有设置变量。

因此 - 将SQL循环之前的变量设置为空白或其他内容:

$dbname = null;
$dbpass = null;
$dbemail = null;

- 编辑 - 或者只是将if语句放在while循环中(如果您只期望最多一个结果......)