我正在做用户个人资料页面,这个必须显示当前信息。但是当我显示页面时,它会显示如下错误: 未定义的变量:第97行的C:\ wamp \ www \ Assignment \ userpage.php中的PhoneNumber
我无法找到问题,对我来说似乎都没问题。
<!doctype html>
<html>
<head>
<?php
header('Content-type: text/html; charset=utf-8');
$link = mysqli_connect("localhost", "root", "");
if (!$link) {
die('Could not connect : '.mysql_error());
}
$db = mysqli_select_db($link, 'assignment');
if (!$db) {
die('Could not select db : '.mysql_error());
}
if ($_SESSION['loggedin'] = true) {
$query = mysqli_query($link, "SELECT UserID FROM users WHERE UserID=.\$_SESSION['UserID']");
}
$UserID = $query;
$result = mysqli_query($link, "SELECT * FROM users where UserID='$UserID'");
while ($row = mysqli_fetch_array($result)) {
global $Lname, $Fname, $PhoneNumber, $Gender, $DOB, $Email;
$Lname = $row['Lname'];
$Fname = $row['Fname'];
$PhoneNumber = $row['PhoneNumber'];
$Gender = $row['Gender'];
$DOB = $row['DOB'];
$Email = $row['Email'];
echo 'nananananana';
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Movie Renting</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body id="wrapper">
<div class="order">
<h1>Таны бүртгэлийн мэдээлэл:</h1>
<form action = "userpage.php" method = "post">
<table frame="box" style="width:900px; height:auto">
<tr>
<td> Овог:</td>
<td> <?php echo $Lname ?></td> </tr>
<tr>
<td> Нэр:</td>
<td> <?php echo $Fname ?></td> </tr>
<tr>
<td> Хэрэглэгчийн нэр:</td>
<td> <?php echo $UserID ?></td> </tr>
<tr>
<td> E-шуудан:</td>
<td> <?php echo $Email ?> </td> </tr>
<tr>
<td> Утасны дугаар:</td>
<td> <?php echo $PhoneNumber ?></td> </tr>
<tr>
<td> Хүйс:</td>
<td> <?php echo $Gender ?></td> </tr>
<tr>
<td> Төрсөн он сар өдөр: </td>
<td> <?php echo $DOB ?></td> </tr>
<a href = "useredit.php">Өөрийн мэдээллээ засах бол энд дарна уу.</a>
</table>
</form>
</div>
</body>
</html>
答案 0 :(得分:1)
变量的范围是定义它的上下文。在大多数情况下,所有PHP变量只有一个范围。此单一范围也涵盖包含和所需文件。
例如:
让我们假设在一个文件example.php中,您有以下内容:
<?php
$foo = "Hello World"; // global scope
function hello() {
echo $foo; // reference to local scope
}
hello();
?>
此脚本不会产生任何输出,因为函数hello()中的echo语句引用$ foo变量的本地版本,并且尚未在此范围内为其分配值。
现在,如果您想从函数hello()中访问已定义或声明的全局范围变量$ foo,那么您需要使用 声明变量$ foo函数中的全局 关键字,例如:
<?php
$foo = "Hello World"; // global scope
function hello() {
global $foo;
echo $foo; // reference to local scope
}
hello();
?>
现在调用函数hello()将产生:
Hello World
while-block在其块中的范围有限,因此,同样的原则不适用,但是当变量不存在于外部时,使用global关键字在while块中定义变量虽然块确实会影响变量。
所以在您目前拥有的while块中:
while ($row = mysqli_fetch_array($result)) {
global $Lname, $Fname, $PhoneNumber, $Gender, $DOB, $Email;
$Lname = $row['Lname'];
$Fname = $row['Fname'];
$PhoneNumber = $row['PhoneNumber'];
$Gender = $row['Gender'];
$DOB = $row['DOB'];
$Email = $row['Email'];
echo 'nananananana';
}
在while-block之外的全局范围内定义变量$ Lname,$ Fname,$ PhoneNumber,$ Gender,$ DOB,$ Email,或者将全局变量放在while-block范围之外,这样你就可以拥有访问它们,例如:
global $Lname, $Fname, $PhoneNumber, $Gender, $DOB, $Email;
while ($row = mysqli_fetch_array($result)) {
$Lname = $row['Lname'];
$Fname = $row['Fname'];
$PhoneNumber = $row['PhoneNumber'];
$Gender = $row['Gender'];
$DOB = $row['DOB'];
$Email = $row['Email'];
echo 'nananananana';
}
或者,我会做这样的事情:
$Lname = '', $Fname = '', $PhoneNumber = '', $Gender = '', $DOB = '', $Email = '';
while ($row = mysqli_fetch_array($result)) {
global $Lname, $Fname, $PhoneNumber, $Gender, $DOB, $Email;
$Lname = $row['Lname'];
$Fname = $row['Fname'];
$PhoneNumber = $row['PhoneNumber'];
$Gender = $row['Gender'];
$DOB = $row['DOB'];
$Email = $row['Email'];
echo 'nananananana';
}
或者即使你不打算调用文件中的其他文件,在这种情况下使用 全局 关键字也是不必要的,你可以在以下方式:
$Lname = '', $Fname = '', $PhoneNumber = '', $Gender = '', $DOB = '', $Email = '';
while ($row = mysqli_fetch_array($result)) {
$Lname = $row['Lname'];
$Fname = $row['Fname'];
$PhoneNumber = $row['PhoneNumber'];
$Gender = $row['Gender'];
$DOB = $row['DOB'];
$Email = $row['Email'];
echo 'nananananana';
}
从效率的角度考虑,为什么要在每个循环迭代循环中调用global关键字?我只是在while循环之外定义/声明变量,并且在迭代过程中将在while循环中添加值的赋值。
变量范围可能会有点棘手,希望这会有所帮助。
答案 1 :(得分:0)
您无需使用全局关键字。
只是在while循环之外声明你的变量。
$PhoneNumber= "";
while ($row = mysqli_fetch_array($result)) {
$Lname = $row['Lname'];
$Fname = $row['Fname'];
$PhoneNumber = $row['PhoneNumber'];
$Gender = $row['Gender'];
$DOB = $row['DOB'];
$Email = $row['Email'];
echo 'nananananana';
}
echo $PhoneNumber;
现在您可以从while循环外部访问$ PhoneNumber。
感谢。
答案 2 :(得分:0)
以下是更正后的代码:
$Lname = '';
$Fname = '';
$PhoneNumber = '';
$Gender = '';
$DOB = '';
$Email = '';
if (!empty($_SESSION['loggedin']))
{
$result = mysqli_query($link,
"SELECT * FROM users WHERE UserID=" . (int)$_SESSION['UserID']
);
if ($row = mysqli_fetch_array($result))
{
$Lname = $row['Lname'];
$Fname = $row['Fname'];
$PhoneNumber = $row['PhoneNumber'];
$Gender = $row['Gender'];
$DOB = $row['DOB'];
$Email = $row['Email'];
}
}