Showing error to user

I have 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.

[php]//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");
}

}
[/php]

[php]
//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
}  

[/php]

Well, error tracking is important. But, in your sample code, you are handling it in two different ways.
First, you create a query that is executed with “mysql_query” function.
If it fails, you stop everything with a “DIE” command.

That should be changed so that if it fails, you could display a page with various options.
One option would be to retry the query again or maybe send an email to you as the webmaster.
(anything but just die!)

Next, you grab a copy of the data from the query using the “mysql_fetch_assoc” function.
But, you didn’t check this for zero data. You should check the number of rows that resulted.
If zero rows, you should display a message somewhere that mentions “No records found!”.
(Or something similar…)

So, the try/catch should work for you if you add in some further “catches” like the query and count issues.

Hope that helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service