无法从PHP中的mysql\u fetch\u array()获取数据
这是我的PHP:这段代码做得很好无法从PHP中的mysql\u fetch\u array()获取数据,php,mysql,Php,Mysql,这是我的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(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();
}
但在同一页的后面,我尝试从$info变量访问数据,结果什么也没有出来。我知道我做错了什么,但不知道是什么
<?php print $info['name']?>
$info变量被本地化为if语句 将$info变量设为全局变量。执行以下操作时: 当最后$info超出最后一条记录时,它为false不再有记录。因此$info为false,而循环终止,其中没有更多的数据库信息 作为一种解决方法,我们将第一个$info保存在$info0中, 在while循环之后使用$info0而不是$info
<?php echo $info0['name']; ?>
在while循环之后,使用以下函数:
$info = mysql_data_seek($check, 0) ? mysql_fetch_array($check) : null;
当然,.如果你想在ifisset$\u COOKIE[user]{中使用$info是可以的,但是如果你想在ifisset$\u COOKIE[user]{之外使用$info,将会有一个问题,因为在ifisset$\u COOKIE[user]{之前没有初始化$info,所以你的代码是这样的
$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 );
您的输出或日志文件中是否有任何警告/错误?不再支持mysql_*函数,它们已不再维护,将来将不再支持。您应该使用或更新代码,以确保将来项目的功能。如果我将变量设为全局变量,如何使用它?$check=mysql\u querySELECT*来自email='$username'或diemsql\u error;$info=mysql\u fetch\u array$check;而$info{}您实际上可以在while语句中设置一个新变量,并将$info['name']设置为与之相等。$name=然后在while语句中:$name=$info['name']
<?php echo $info0['name']; ?>
$info = mysql_data_seek($check, 0) ? mysql_fetch_array($check) : null;
$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 );