0

I have this code:

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;

        }

I got this message in the browser :

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

those two lines refers to the folowing peice of code:

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

even though I have already had Name and password in my database.

I am really confused would you help me please.

4
  • Try to run print_r($row);die(); right at the beginning of your while loop. Commented Jul 9, 2013 at 17:19
  • I got this Array ( [0] => 2 [1] => 0 [2] => ww ) Commented Jul 9, 2013 at 17:21
  • Then that's all the data you have in your row. No Name or password to be found. Try using mysql_fetch_assoc() instead of mysql_fetch_row(), if you truly must use the (deprecated) mysql_* extension. Commented Jul 9, 2013 at 17:22
  • Those are not fatal errors, only notices to let you know that you have not initialized those variables prior to using them. Make sure your character case is correct - name vs Name, etc.. Commented Jul 9, 2013 at 17:23

1 Answer 1

3

mysql_fetch_row is only for fetching array with numerical key.
try using mysql_fetch_assoc instead

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

into

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

in line 104

Sign up to request clarification or add additional context in comments.

4 Comments

the one with the problem is not in that line, but down below
Funny, he's using mysql_fetch_array further upwards, but not where it would matter. :)
your answer is very helpful, i still have a problem, the query gives me results which don't validate the $username which i have already entered
@user2274019 It might be better to just accept this answer if it solves your problem, and ask a new question for a new problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.