简体   繁体   English

无法从PHP中的mysql_fetch_array()获取数据

[英]Can't fetch data from mysql_fetch_array() in PHP

Here is my PHP: (that code does it's job well) 这是我的PHP :(该代码确实很好)

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. 但后来在同一页面上,我尝试从$ info变量访问数据,但没有任何结果。 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.....???)
        {
                        }

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. 当它超出最后一条记录时,最后的$ info为false(不再记录)。 So $info is false, the while loop terminates, there is no more database "info" in it. 所以$ info是false,while循环终止,其中没有更多的数据库“info”。

As a workaround, we save the first $info in $info0 , 作为解决方法,我们将$ info0中的第一个$ info保存,

$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. 在while循环之后使用$ info0而不是$ info。

<?php echo $info0['name']; ?>

The $info variable is localized to the if statement. $ info变量已本地化为if语句。

Make the $info variable global. 使$ info变量全局化。

After the while loop, use the mysql_data_seek function: while循环之后,使用mysql_data_seek函数:

$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_ . 当然, 你必须避免使用所有以mysql_开头的PHP函数

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 如果你想在if(isset($_COOKIE["user"])) {使用$info if(isset($_COOKIE["user"])) {没关系,但如果你想使用外面的$info if(isset($_COOKIE["user"])) {会有一个问题因为$info没有在if(isset($_COOKIE["user"])) {之前初始化if(isset($_COOKIE["user"])) {所以你的代码会是这样的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM