List table data and assign value to each of them (SQL)

Asked

Viewed 98 times

0

Good morning! I would like to know how I do to list the data of a table and assign value to each of them! Example:

Table: character Columns: ID and name

I would like to take all the names that have the same id, and list them on a page each with the value of their own name! I do not know if it was clear :x I Await Answers.

  • How can multiple users have the same ID ?

  • Because this id is in case the account ID, so one account can have several characters understand? :D

  • Okay, I still don’t fully understand :/, but I’ll write a code.

  • Like this I’ll explain better: Each user account has an account_id. However, each account can have more than one character, who will be saved with the account_id id id to know which account they are linked to, but with the name of the character who is. Example: Account account_id 1 with the characters player 1 and player 2. What I want to do, is to list all the players assigned to an account and apply to them the id and name of their own names, that is, player and player 2 so that I can make changes to each one’s column. I don’t know if it’s clearer now :X

1 answer

1


Hello, it would be something like that ?

$sql = "SELECT id FROM personagens where id_usuario={$id_usuario_logado}";

$sql = mysqli_query($conexao_sqli, $sql);

$id = "";

while($row = mysqli_fetch_object($sql)){
    $id .= $row->id . ",";
}

$id = substr($id, 0 , -1);

$sql = "SELECT usuario FROM personagens WHERE id IN({$id})";

$sql = mysqli_query($conexao_sqli, $sql);

while($row = mysqli_fetch_object($sql)){
    echo $row->usuario ."<br/>";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.