Jump to content

xXGriMe

New Members
  • Posts

    5
  • Joined

  • Last visited

xXGriMe's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thanks for the advice. I am going to go back and rethink how this data should be sent and revived. I originally was going to just delete rows not used by the player in player_deck rather than having -1 be a empty card slot but i figured that it would increase the latency from client to database. I also thought about what would happen if say the server crashed while the database was deleting and inserting rows. If that happened the player could potentially lose a account that they spent hours on. Regardless thank everyone in here for spending time trying to help me. If I have more questions ill be back.
  2. When a player registers in the game 60 rows are created in player_deck for that player. You can think of each of those rows as a slot for a card. The player will always ever have 60 rows because that is the max amount of unique cards a player is able to have in the deck. The first 40 slots are auto populated with cards for the player. So now player_deck will look something like this. player_deck row_id player_id card_id 1 34 78 row_id player_id card_id 2 34 66 row_id player_id card_id 3 34 34 row_id player_id card_id 4 34 -1 row_id player_id card_id 5 34 -1 A row with -1 as the card id means there is no card in that slot. So by looking at that i can see the player has 3 cards. Now in the game I load those cards from the database. From the game the player is able to add or remove cards if they like. When the player saves the deck from the game it calls my php page with the new cards the player changed. Something like this might be sent to the php page. $playerDeckCardIds=$_GET['deckcardids']; //This contains a string that looks something like this. "67 98 3 66 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 78 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 " From that I can see the player sent a deck that has 43 unique cards and 17 card slots that are empty. $deckCardIds splits that string up into a array and I remove the last entry in the array. So $deckCardIds should have a index of 59. $deckCardIds = explode(" ", $playerDeckCardIds); array_pop($deckCardIds); Then I select the 60 rows from the database I need based on the player_id. $stmt = $conn->prepare('SELECT row_id FROM player_deck WHERE player_id = :player_id'); $stmt->execute(array('player_id' => $playerId)); $r = $stmt->fetchAll(PDO::FETCH_OBJ); Loop through those rows. Each iteration the loop with increase the index of $deckCardIds updating the new card_id on the loops current row_id. foreach($r as $row) { $stmt = $conn->prepare('UPDATE player_deck SET card_id = :card_id WHERE row_id = :row_id'); $stmt->bindParam(':card_id', $deckCardIds[$i]); $stmt->bindParam(':row_id', $row->row_id); $stmt->execute(); $i++; } After all this runs the card_id for all 60 rows in the database gets set to 0 though. I feel really bad for not being able to explain this better for you guys. I do appreciate you taking your time to try and help me though.
  3. Ok so I changed the way my code is working a little bit. Im selecting the rows I need based on the row_id that matches the player_id. Then im updating the card_ids in the database with what i have in $deckCardIds based on the current row_id we are on in our loop. Problem is for some reason every card_id in the database gets set to 0 when it finishes. I can see that its getting set but then it is changed back to 0 after the loop is done. I dont get whats going on here. <?php $playerId=$_GET['playerid']; $playerDeckCardIds=$_GET['deckcardids']; $deckCardIds = explode(" ", $playerDeckCardIds); array_pop($deckCardIds); try { #Connect to the db $conn = new PDO('mysql:host=localhost;dbname=tcg', 'root', ''); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare('SELECT row_id FROM player_deck WHERE player_id = :player_id'); $stmt->execute(array('player_id' => $playerId)); $r = $stmt->fetchAll(PDO::FETCH_OBJ); $i=0; foreach($r as $row) { $stmt = $conn->prepare('UPDATE player_deck SET card_id = :card_id WHERE row_id = :row_id'); $stmt->bindParam(':card_id', $deckCardIds[$i]); $stmt->bindParam(':row_id', $row->row_id); $stmt->execute(); $i++; } } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); } ?>
  4. Im not sure thats what im looking for. Sorry im not explaining this very well. When a player registers from Unity the table player_deck gets populated with 60 rows. Each row represents a card that can exist in the players deck. These 60 rows are unique to that player based on the players id. Now from Unity the player is able to able to remove or add cards from the deck. When the player saves the deck it calls my php page. $playerDeckCardIds=$_GET['deckcardids']; //This contains a string that looks something like this. "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 " $deckCardIds = explode(" ", $playerDeckCardIds); //Split the string into a array array_pop($deckCardIds); //Remove the last entry because it holds no id Now I have the new card ids stored in a array $deckCardIds that need to be placed in the database. #Connect to the db $conn = new PDO('mysql:host=localhost;dbname=tcg', 'root', ''); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Select the rows i need to change $stmt = $conn->prepare('SELECT card_id, card_amount FROM player_deck WHERE player_id = :player_id'); $stmt->execute(array('player_id' => $playerId)); //$r now holds a array of those rows $r = $stmt->fetchAll(PDO::FETCH_OBJ); //$i is my index $i = 0; //Loop through the rows that need to be changed foreach($r as $row) { //This update statement needs to represent the current row im looping through $stmt = $conn->prepare('UPDATE player_deck SET card_id = :card_id WHERE player_id = :player_id'); //Wrong $stmt->bindParam(':player_id', $playerId); $stmt->bindParam(':card_id', $deckCardIds[$i]); $stmt->execute(); $i++; } I know my update statement is wrong here but im not sure how to only update the row im currently looping through.
  5. Im developing a trading card game in unity and need some help with the php and mysql side of things. My database structure is something like this. This is set up when the player registers. Each player has 60 rows inside of player_deck because the decks limit is 60. players player_id/email/password/username player_deck player_id/card_id/card_amount Now this is where I need help. Im trying to update these rows when the player saves the deck. From Unitys end im calling a URL that looks something like this. "localhost/saveplayerdata/saveplayerdeck.php?playerid=&deckcardids=&deckcardamounts=" The PHP page stores the deckcardids and deckcardamounts into two arrays. Now here is where im stuck. I need to get all the rows that belong to that player and update them with our data sent from Unity. This is what i have so far. Im able to select the rows I need but how would I go about updating them with the data in my arrays? Thanks for reading. <?php $playerId=$_GET['playerid']; $playerDeckCardIds=$_GET['deckcardids']; $playerDeckCardAmounts=$_GET['deckcardamounts']; $deckCardIds = explode(" ", $playerDeckCardIds); array_pop($deckCardIds); $deckCardAmounts = explode(" ", $playerDeckCardAmounts); array_pop($deckCardAmounts); //This is the new array of cardIds sent from Unity //foreach ($deckCardIds as $value) //{ //echo "Deck Card Id $value <br>"; //} //This is the new array of cardAmounts sent from Unity // foreach ($deckCardAmounts as $value) //{ //echo "Deck Card Amount $value <br>"; //} try { #Connect to the db $conn = new PDO('mysql:host=localhost;dbname=tcg', 'root', ''); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare('SELECT card_id, card_amount FROM player_deck WHERE player_id = :player_id'); $stmt->execute(array('player_id' => $playerId)); $r = $stmt->fetchAll(PDO::FETCH_OBJ); #If we have a result if ( count($r) ) { foreach($r as $row) { //echos the current database information. I need to update this data with the data inside $deckCardIds and $deckCardAmounts. echo $row->card_id; echo $row->card_amount; } } #We did not any rows that matched our id else { echo 'No match'; } } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); } ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.