• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

[Question] How can you link a pokemon to a persons account?

16
Posts
7
Years
  • Age 27
  • Seen Jul 31, 2019
So if your wondering the content to this, I have been making a pokemon online rpg, and I am still setting up the database, but while setting up said database, I need help with trying to figure out some things, mostly with catching pokemon and having pokemon in your inventory. I'm gonna have to figure out how to look at pokemon too, but right now I'm more worried of the captured pokemon being linked to the persons account, so they can use it in battles, or for better words, how to have the captured pokemon with the trainer and not just have on screen you captured it and not have the pokemon linked to the account, if that makes sense. same goes for poke balls and items, since they fall under a similar category. do I add something like $link pokemon.png to user0 or die( capture failed) ?
or am I like getting the idea but I'm not getting the exact code?

Thank you!
 
1,403
Posts
9
Years
  • Seen today
There's lots of things you could do here, but one of the more traditional ones is to store the information about players and what they own in your database.

If you have a table for a player that looks something like this:

Code:
CREATE TABLE Player
  ( id INT NOT NULL AUTO_INCREMENT
  , email VARCHAR(40) NOT NULL
  , password_hash VARCHAR(32) NOT NULL -- NOTE: Look into hashing passwords with salt.
  , name VARCHAR(16) NOT NULL
  , PRIMARY KEY (id)
  );

Then you can associate other data with that player by giving the other data a foreign key into Player.player_id:

Code:
CREATE TABLE Pokemon
  ( species INT NOT NULL -- e.g. 1 = Bulbasaur. If you have a different scheme, use that.
  , nickname VARCHAR(16)
  , ... -- Things like level, EVs, moves, etc can go here.
  , owner INT NOT NULL
  , FOREIGN KEY (owner) REFERENCES Player (id)
  );

To give a player a Pokémon I could execute a query that looks like this (assuming you're using MySQLi in PHP—I'd recommend it, because consistent use of bind_param will save you from SQL injection):
Code:
/* Assuming we have $mysqli connected to a database, and $species, $nickname, $player_id set to appropriate values. */
$stmt = $mysqli->prepare("INSERT INTO Pokemon (species, nickname, owner) VALUES (?, ?, ?)") or die($mysqli->error);
$stmt->bind_param("i", $species) or die($stmt->error);
$stmt->bind_param("s", $nickname) or die($stmt->error);
$stmt->bind_param("i", $owner) or die($stmt->error);
$stmt->execute() or die($stmt->error);

And to get the list of Pokémon a player owns you can do this:
Code:
$stmt = $mysqli->prepare("SELECT * FROM Pokemon WHERE owner=?") or die($mysqli->error);
$stmt->bind_param("i", $player_id) or die($stmt->error);
$stmt->execute() or die($stmt->error);
$res = $stmt->get_result() or die($stmt->error);
while ($row = $res->fetch_assoc()) {
  /* Do something with each Pokémon. */
  echo $row["nickname"];
}

(I haven't run any of the code, so there could be some errors but hopefully this will point you in a direction!)
 
16
Posts
7
Years
  • Age 27
  • Seen Jul 31, 2019
This is super helpful! If there are any bugs in it ill debug them! Thank you so much!
And ill credit you for the code, this is super helpful! QwQ
 
Back
Top