Showing error message to user

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.

//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 ['korisnickoime']; ?>'>
            <img src="avatar/<?php echo $kor_avatar['avatar']; ?>" height='100' width='100' alt='avatar'></a>
        <?php
    }

Hi godflesh, welcome to the forums!

First of all, I’d recommend against using the mysql_* functions. The mysql extension has been deprecated and will be removed from PHP in a future release, and it also has security issues. It would be better to switch over to using either the mysqli or PDO extension.

In this case, you want to be able to continue rendering your page, even if there’s an error fetching usernames/avatars, so you should avoid using die() statements in your code. It would be better to catch any errors within your take_username_avatar() function (this would be simpler if using PDO, as you can just use one try/catch block to catch all errors) and return an empty array. In your template you’d then check the return value and display an error message if it’s empty.

Hello fretburner, thanks for the warm welcome and for the reply. :slight_smile:

Yes, PDO is the way to go, I have been looking for tutorials on PDO and I have found a few (if you have a tutorial to recommend please do) but for this project I’m nearly finished and this is just for the show, it won’t be active online.

I will try to do as you advised me, to modify the take_username_avatar() function so it could return empty array.

I have modified the code but it still won’t do what I want it to do.

//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){
        if(empty($results)){
          echo "The list cannot be displayed";
      }
    }
        
}

Try something like this:


function take_username_avatar()
{
    $results = array();
    $query = "SELECT username,avatar FROM data WHERE username!='{$_SESSION['username']}'";

    if ($result = mysql_query($query)) {
        while($row = mysql_fetch_assoc($result)) {
            $results[] = $row;
        }
    }

    return $results;
}

and then in your template:


<?php
$username_avatar = take_username_avatar();

if ($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['korisnickoime']; ?>">
        <img src="avatar/<?php echo $kor_avatar['avatar']; ?>" height='100' width='100' alt='avatar'></a>

<?php
    }
} else {
?>
    <p>The list cannot be displayed.</p>
<?php
}

Yes !
That’s the one !

Thank you fretburner.