user3000199
user3000199

Reputation: 1

Can't fetch data from mysql_fetch_array() in PHP

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.....???)
        {
                        }

Upvotes: 0

Views: 89

Answers (4)

Haikal
Haikal

Reputation: 39

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 );

Upvotes: 0

evalarezo
evalarezo

Reputation: 1153

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_.

Upvotes: 0

jacouh
jacouh

Reputation: 8741

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']; ?>

Upvotes: 2

Hunter
Hunter

Reputation: 146

The $info variable is localized to the if statement.

Make the $info variable global.

Upvotes: 0

Related Questions