Reputation: 1
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
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
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
Reputation: 8741
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.
$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
Reputation: 146
The $info variable is localized to the if statement.
Make the $info variable global.
Upvotes: 0