0

Here is my PHP: (that code does it's job well)

if(isset($_COOKIE["user"])) {
$username = $_COOKIE["user"]; 
$pass = $_COOKIE["password"];
$check = mysql_query("SELECT * FROM members WHERE email = '$username'")or die(mysql_error()); 
    while ($info = mysql_fetch_array( $check ))
        {
        //if the cookie is present but has the wrong password, they are  taken to the login page 
        if ($pass != $info['password']) 
        {
            header("Location: login.php"); 
            exit();
        }  
        else //if the cookie is present and doesn'T have the wrong password they are shown the admin area    
        { 
            include 'header.php';
        }
    }

}
else //if the cookie is present and doesn'T have the wrong password they are shown the admin area    
        {
            header("Location: login.php"); 
            exit();
        }   

but later on the same page, I try to access data from the $info variable and nothing comes out. I know i'm doing something wrong, but can't figure out what...

<?php print $info['name']?>

If I make my variable global, how do I use it the while ?

$check = mysql_query("SELECT * FROM members WHERE email = '$username'")or die(mysql_error());
$info = mysql_fetch_array( $check );
    while ($info.....???)
        {
                        }
  • Are you getting any warnings/errors in your output or in your log files? – Justin Wood Nov 18 '13 at 21:51
  • 2
    There is **no more support** for `mysql_*` functions, they are [**officially deprecated**](https://wiki.php.net/rfc/mysql_deprecation), **no longer maintained** and will be [**removed**](http://php.net/manual/en/function.mysql-connect.php#warning) in the future. You should update your code with [PDO](http://php.net/pdo) or [MySQLi](http://php.net/msqli) to ensure the functionality of your project in the future. – War10ck Nov 18 '13 at 21:55

4 Answers4

2

When you do:

while ($info = mysql_fetch_array( $check ))
{
   //
   // do things with info:
   //...
   //
}

The last $info is false (no more record), when it reaches beyond the last record. So $info is false, the while loop terminates, there is no more database "info" in it.

As a workaround, we save the first $info in $info0,

$info0 = false;

while ($info = mysql_fetch_array( $check ))
{
  if(! $info0)
  {
    $info0 = $info;
  }
   //
   // do things with info:
   //...
   //
}

you use $info0 instead of $info after the while loop.

<?php echo $info0['name']; ?>
Community
  • 1
  • 1
jacouh
  • 8,473
  • 5
  • 32
  • 43
0

The $info variable is localized to the if statement.

Make the $info variable global.

Hunter
  • 146
  • 4
  • If I make my variable global, how do I use it the while ? $check = mysql_query("SELECT * FROM members WHERE email = '$username'")or die(mysql_error()); $info = mysql_fetch_array( $check ); while ($info) { } – user3000199 Nov 18 '13 at 22:10
  • You could actually just set up a new variable and set `$info['name']` equal to it inside your while statement. – Hunter Nov 18 '13 at 22:15
  • `$name=''` then in the while statement: `$name=$info['name']` – Hunter Nov 18 '13 at 22:17
0

After the while loop, use the mysql_data_seek function:

$info = mysql_data_seek($check, 0) ? mysql_fetch_array($check) : null;

Of course, you MUST avoid to use all PHP functions that begin with mysql_.

Community
  • 1
  • 1
evalarezo
  • 1,134
  • 7
  • 13
0

if you want use $info in if(isset($_COOKIE["user"])) { is okay but if you want to use $info outside if(isset($_COOKIE["user"])) { there will be a problem becaause $info not initialization before if(isset($_COOKIE["user"])) { so your code will be like this

$username = $_COOKIE["user"]; 
$pass = $_COOKIE["password"];

if(isset($_COOKIE["user"])) {
$check = mysql_query("SELECT * FROM members WHERE email = '$username'")or die(mysql_error()); 
    while ($info = mysql_fetch_array( $check ))
        {
        //if the cookie is present but has the wrong password, they are  taken to the login page 
        if ($pass != $info['password']) 
        {
            header("Location: login.php"); 
            exit();
        }  
        else //if the cookie is present and doesn'T have the wrong password they are shown the admin area    
        { 
            include 'header.php';
        }
    }

}
else //if the cookie is present and doesn'T have the wrong password they are shown the admin area    
        {
            header("Location: login.php"); 
            exit();
        }   


$check = mysql_query("SELECT * FROM members WHERE email = '$username'")or die(mysql_error()); 
$info = mysql_fetch_array( $check );
Haikal
  • 39
  • 1
  • 9