PHP检查字符串是否在表中

So i've been trying to sort this issue out for a while but been unable to figure out if i have to do this trough SQL side or the PHP side.

My code i have so far:

<?php
$servername="";
$username = "";
$password = "";
$dbname = "alphacre_kingsland";     


$conn = mysqli_connect($servername, $username, $password, $dbname);
    if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "SELECT * FROM `bm_player_bans` ORDER BY id";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result)>0){
        while($row = mysqli_fetch_assoc($result)){
            $playerId = bin2hex($row['player_id']);
            $storedname= getPlayerName($playerId);
            $sql = "INSERT INTO bm_onlinedata (uuid, playername) VALUES ('".$playerId."', '".$storedname."')";

            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
                } else {
                    echo "Error: " . $sql . "<br>" . $conn->error;
                }
            }
    }else{
        echo "0 results";
    }

            mysqli_close($conn);

    function getPlayerName($uuid){
        $json_response = file_get_contents("https://sessionserver.mojang.com/session/minecraft/profile/".$uuid);
        $data = json_decode($json_response);
        return $data->name;
    }

?>

So this stores "UUID" and playername into a seperate table. But i want to check if the UUID is already in there before it adds a new one. Basicly updating it. This is because playernames can change. And i don't want multiple rows of the same data. But thats ONLY if its there ofc.

Background information: I'm fetching the username from a user trough Mojangs API, wich only allows a single lookup of that UUID every minute. So i want to get the data every 5 minutes or so using a scheduled task running this script and store it in a table.

You can easily check with select

$sql = "SELECT * FROM `bm_player_bans` ORDER BY id";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result)>0){
    while($row = mysqli_fetch_assoc($result)){
        $playerId = bin2hex($row['player_id']);
        $storedname= getPlayerName($playerId);
        $sql =  "SELECT * FROM `bm_onlinedata` where uuid = '" . $playerId ."';";
        $result = mysqli_query($link, $sql);
        $num_rows = mysqli_num_rows($result);
        if  ($num_rows> 0) { 
            $sql = "UPDATE bm_onlinedata 
                    set playername = '". $storedname."'
                    where uuid = '" .$playerId ."';";   )";
        } else {
         $sql = "INSERT INTO bm_onlinedata (uuid, playername) VALUES ('".$playerId."', '".$storedname."')";

            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
                } else {
                    echo "Error: " . $sql . "<br>" . $conn->error;
                }
        }
    }
}else{
    echo "0 results";
}