0

I've been trying to sort this issue out for a while but been unable to figure out if I have to do this through 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 separate table. But I want to check if the UUID is already in there before it adds a new one. Basically updating it. This is because playernames can change. And I don't want multiple rows of the same data. But that's ONLY if it is there of course.

Background information: I'm fetching the username from a user through Mojang's API, which 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.

2
  • you need to add a WHERE clause to SELECT in order for num_rows to work. Commented Feb 12, 2017 at 20:02
  • You can use INSERT .. ON DUPLICATE KEY UPDATE .. syntax. Commented Feb 12, 2017 at 21:03

1 Answer 1

1

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";
}
Sign up to request clarification or add additional context in comments.

4 Comments

There seems to be something wrong with your usage of " and ' somewhere. I'll see if i can sort it out.
@DarkEyeDragon . i don't use and in the query .. because seems don't need
i ment the ' not the and xD Anyway. Still doesn't seems to work. it keeps adding a row instead of updating it.
Answer updated adding the check on the numbers of rows returned by query .. hope is useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.