godflesh Posted March 20, 2014 Share Posted March 20, 2014 //function that takes username and avatar function take_username_avatar() { try{ $results = array(); $query = mysql_query("SELECT username,avatar FROM data WHERE username!='{$_SESSION['username']}'")or die(mysql_error()); while($row = mysql_fetch_assoc($query)) { $results[] = $row; } return $results; } catch (Exception $e){ die("Error showing user list"); } } //page that shows user list $username_avatar = take_username_avatar(); foreach ($username_avatar as $user_avatar ) { ?> <p><a href='index.php?page=profile& username =<?php echo $user_avatar [' username ']; ?>'> <?php echo $kor_avatar[' username '] ?></a></p> <a href='index.php?page=profile&username=<?php echo $ user_avatar ['username']; ?>'> <img src="avatar/<?php echo $kor_avatar['avatar']; ?>" height='100' width='100' alt='avatar'></a> <?php } I have been trying to solve this for some time, even too long and I would really appreciate the help of experienced PHP user. I have made that user can see the list of the other members on the website but I also want to show error message if something goes wrong. I did it with try-catch block but I also been wondering if there is an efficient way to do that. I would also like to know how to make the error to happen because I need to make a print screen for documentation. Quote Link to comment https://forums.phpfreaks.com/topic/287113-showing-error-message-to-user/ Share on other sites More sharing options...
Ch0cu3r Posted March 20, 2014 Share Posted March 20, 2014 (edited) Are you wanting to display an error message if the query does not return the users avatar? If you so you should code your function like //function that takes username and avatar // pass the username to the function function take_username_avatar($username) { $result = mysql_query("SELECT username, avatar FROM data WHERE username ='$username'")or die(mysql_error()); if($result)) { if(mysql_num_rows($result)) { return mysql_fetch_assoc($query); // return the username and avatar } } return false; // any other case return false } You'd then check the return value of the function to decided whether to display an error $username_avatar = take_username_avatar($_SESSION['username']); // if the take_username_avatar() returned false, display error if($username_avatar === false) { echo $_SESSION['username'] . ' has no avatar'; } // diplay the avatar else { ?> <p><a href='index.php?page=profile& username =<?php echo $username_avatar[' username ']; ?>'> <?php echo $username_avatar[' username '] ?></a></p> <a href='index.php?page=profile&username=<?php echo $username_avatar['username']; ?>'> <img src="avatar/<?php echo $username_avatar['avatar']; ?>" height='100' width='100' alt='avatar'></a> <?php } ?> would also like to know how to make the error to happen because I need to make a print screen for documentation. To force the error so you can take a screenshot of it pass non existing user to the function, eg take_username_avatar('non-existing-user') and the function will return false. Which will force the error message to be displayed Edited March 20, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/287113-showing-error-message-to-user/#findComment-1473295 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.