我有一个名为user_bio
的表,它有以下列:
id
age
studying
relationship_status
username
about_me
目前,该表有一行用于用户conor
:
id; 1
age: 30
studying: test
relationship_status: single
username: conor
about_me: test
如果我以Conor
的身份登录并转到bio.php/conor
,则conor的bio将显示良好。但是,如果我以Alice
的身份登录并转到bio.php/alice
,除了年龄之外,我会得到未定义的变量错误,年龄显示正确(每个用户都有正确的值)。
这是我的代码,以及我如何显示数据:
<?php
/****************************************/
// Getting data to display on bio page for user
$get_bio = mysqli_query($connect, "SELECT * FROM user_bio WHERE username ='$user'");
$row_returned = mysqli_num_rows($get_bio);
if ($row_returned == 1){
while ($get_bio_data = mysqli_fetch_assoc($get_bio)){
$about_me = $get_bio_data['about_me'];
$age = $get_bio_data['age'];
$studying = $get_bio_data['studying'];
$lang = $get_bio_data['language'];
$rel_status = $get_bio_data['relationship_status'];
/*****************************************/
// change data held by vars
// if no bio for the user exists in the db (no rows in db)s
if ($row_returned == 0){
if ($about_me == ""){
$about_me = "None set..";
} else {
$about_me = $get_bio_data['about_me'];
} // else closed
} // if closed
} // while closed
} // if closed
/*********************************************/
?>
<div id="userposts_panel">
<div class="bio_here">
<p> <b> About me: </b> <?php echo $about_me; ?> </p>
<p> <b> Age: </b> <?php echo $age; ?> </p>
<p> <b> Studying: </b> <?php echo $studying; ?> </p>
<p> <b> Language(s): </b> <?php echo $lang; ?> </p>
<p> <b> Relationship Status: </b> <?php echo $rel_status; ?> </p>
</div>
</div>
我认为,因为数据库中没有对应于username
alice的行,这就是导致错误的原因。在这种情况下,我想重新定义我所选字符串的变量,例如,如果用户存在about_me
,则显示它,否则$about_me将等于none set
。但我仍然收到未定义的错误?
如果没有用户Alice,就不能进入if ($row_returned == 1){
。因此,在此之前,您需要初始化变量($about_me,$age,…)if.
试试这个代码:
<?php
/****************************************/
// change data held by vars if no bio for the user exists in the db (no rows in db)s
$about_me = "None set..";
$age = "None set..";
$studying = "None set..";
$lang = "None set..";
$rel_status = "None set..";
// Getting data to display on bio page for user
$get_bio = mysqli_query($connect, "SELECT * FROM user_bio WHERE username ='$user'");
$row_returned = mysqli_num_rows($get_bio);
if ($row_returned == 1){
while ($get_bio_data = mysqli_fetch_assoc($get_bio)){
$about_me = $get_bio_data['about_me'];
$age = $get_bio_data['age'];
$studying = $get_bio_data['studying'];
$lang = $get_bio_data['language'];
$rel_status = $get_bio_data['relationship_status'];
/*****************************************/
} // while closed
} // if closed
/*********************************************/
?>
<div id="userposts_panel">
<div class="bio_here">
<p> <b> About me: </b> <?php echo $about_me; ?> </p>
<p> <b> Age: </b> <?php echo $age; ?> </p>
<p> <b> Studying: </b> <?php echo $studying; ?> </p>
<p> <b> Language(s): </b> <?php echo $lang; ?> </p>
<p> <b> Relationship Status: </b> <?php echo $rel_status; ?> </p>
</div>
</div>
您可以使用isset()方法检查变量是否设置如下:
<div id="userposts_panel">
<div class="bio_here">
<p> <b> About me: </b> <?php if(isset($about_me)){ echo $about_me;} else{ echo "none";} ?> </p>
<p> <b> Age: </b> <?php if(isset($age)){ echo $age;} else{echo "none";} ?> </p>
<p> <b> Studying: </b> <?php if(isset($studying)){ echo $studying;} else{ echo "none";} ?> </p>
<p> <b> Language(s): </b> <?php if(isset($lang)){ echo $lang;} else { echo "none";} ?> </p>
<p> <b> Relationship Status: </b> <?php if(isset($rel_stautus)){ echo $rel_status;} else{ echo "none";} ?> </p>
</div>
试试这个:
<?php
/****************************************/
// Getting data to display on bio page for user
$get_bio = mysqli_query($connect, "SELECT * FROM user_bio WHERE username ='$user'");
$row_returned = mysqli_num_rows($get_bio);
if ($row_returned == 1){
while ($get_bio_data = mysqli_fetch_assoc($get_bio)){
$about_me = $get_bio_data['about_me'];
$age = $get_bio_data['age'];
$studying = $get_bio_data['studying'];
$lang = $get_bio_data['language'];
$rel_status = $get_bio_data['relationship_status'];
} // while closed
} // if closed
else {
$about_me = "Not set...";
$age = "Not set...";
$studying = "Not set...";
$lang = "Not set...";
$rel_status = "Not set...";
} // else closed
/*********************************************/
?>
<div id="userposts_panel">
<div class="bio_here">
<p> <b> About me: </b> <?php echo $about_me; ?> </p>
<p> <b> Age: </b> <?php echo $age; ?> </p>
<p> <b> Studying: </b> <?php echo $studying; ?> </p>
<p> <b> Language(s): </b> <?php echo $lang; ?> </p>
<p> <b> Relationship Status: </b> <?php echo $rel_status; ?> </p>
</div>
</div>
<?php
function checkValue($getValue) {
return isset($getValue) ? $getValue : "N/A";
}
?>
<div id="userposts_panel">
<div class="bio_here">
<p> <b> About me: </b> <?php echo checkValue(&$about_me); ?> </p>
<p> <b> Age: </b> <?php echo checkValue(&$age); ?> </p>
<p> <b> Studying: </b> <?php echo checkValue(&$studying); ?> </p>
<p> <b> Language(s): </b> <?php echo checkValue(&$lang); ?> </p>
<p> <b> Relationship Status: </b> <?php echo checkValue(&$rel_status); ?> </p>
</div>
</div>
您可以使用&
作为无知,并使用一个函数来检查是否设置了值,如果没有设置,则显示一些所需的文本。