php - Giving a variable two values if no row exists in the database -


i have table called user_bio, has following columns:

   id    age    studying    relationship_status    username    about_me 

currently, table has 1 row user conor:

   id; 1    age: 30    studying: test    relationship_status: single    username: conor    about_me: test 

if log in conor , go bio.php/conor, bio conor display fine. but, if log in alice , go bio.php/alice, undefined variable errors except age, shows correctly (with correct value each user).

here code, , how displaying data:

<?php /****************************************/ // getting data display on bio page user $get_bio = mysqli_query($connect, "select * user_bio 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 vars          // if no bio user exists in 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> 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> 

i think because there no row in database corresponding username alice, causing error. in case want redefine variable selected string, example, if about_me exists user, display it, else $about_me equal none set. still getting undefined errors?

you not enter if ($row_returned == 1){ if there no user alice. need initialize variables ($about_me, $age, ...) before if.

try code:

<?php /****************************************/  // change data held vars if no bio user exists in 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 display on bio page user $get_bio = mysqli_query($connect, "select * user_bio 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> 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> 

Comments