获取行中的php错误

时间:2013-07-09 17:16:16

标签: php

我有这段代码:

class user
 {

  public $name ;
  public $password ;




    //constructor
    function __construct($username, $password)
    {

    $dbhost = 'localhost';
   $dbuser = 'root' ;
   $dbpass = '' ;
   $db = 'privelidge';
   $conn = mysql_connect($dbhost,$dbuser,$dbpass);
   mysql_select_db($db);

     $sql= "select * from `user` where Name = '".$username."' and  password = '".$password."'" ;
     $result = mysql_query($sql);

        $found  = false;
    while(  $row = mysql_fetch_array($result)){
    $this->name = $username ;
        $this->password = $password ;
        $found = true;
        }
    }

    //method add user
     public static function  add_user( $username , $password )
    {

    $dbhost = 'localhost';
   $dbuser = 'root' ;
   $dbpass = '' ;
   $db = 'privelidge';
   $conn = mysql_connect($dbhost,$dbuser,$dbpass);
   mysql_select_db($db);

     $u = new User($username, $password);

     $u->name = $username ;
     $u->password = $password ;
     $u = User::search($username);

     if($u == null )
     {
       $sql = "INSERT INTO `user` VALUES ('".$username."', '".$password."')";
       mysql_query($sql);

       echo " user has been added ";
       exit;
     }
     else
     {
       echo " username already existed ";
       exit;
     }

    }


        public static function search($username)
        {

            $dbhost = 'localhost';
   $dbuser = 'root' ;
   $dbpass = '' ;
   $db = 'privelidge';
   $conn = mysql_connect($dbhost,$dbuser,$dbpass);
   mysql_select_db($db);


          $sql = " SELECT * FROM `user` WHERE Name = '".$username."'" ;
          $results = mysql_query($sql);

          while($row = mysql_fetch_row($results)){
          $name = $row['Name'];
          $password = $row['password'];
          $user = new user($name, $password);
          return $user;
          }
          return null;

        }

我在浏览器中收到此消息:

Notice: Undefined index: Name in C:\xampp\htdocs\proj\Document1.php on line 105

Notice: Undefined index: password in C:\xampp\htdocs\proj\Document1.php on line 106

这两行代表下面的代码:

$name = $row['Name'];
$password = $row['password'];

即使我的数据库中已经有Namepassword

我真的很困惑你能不能帮我。

1 个答案:

答案 0 :(得分:3)

mysql_fetch_row 仅用于使用数字键获取数组 尝试使用 mysql_fetch_assoc 而不是

while($row = mysql_fetch_row($results)){

进入

while($row = mysql_fetch_assoc($results)){  

第104行