Jump to content

I cannot get username from the second table


Go to solution Solved by Barand,

Recommended Posts

Hello. Im just starting my adventure with PHP and SQL. I was making the "galaxy view" in the form of table on my test site, that looks like this right now. My code looks like this:

<?php
$testowanaGalaktyka = 1;
$testowanySystem = 1;
$testowanaPlaneta = 1;
$testowanyGracz = -1;

$sql = "SELECT planets_ownerid FROM planets WHERE planets_galaxy = $testowanaGalaktyka AND planets_starsystem = $testowanySystem AND planets_planet = $testowanaPlaneta";
$testowanyGracz = array($row["planets_ownerid"]);

$result = $conn->query($sql);

if($result->num_rows>0)
{
   while($row = $result->fetch_assoc())
   {
      echo '<b>'.$row["planets_ownerid"]."</b>";
   }
}
else
{
   echo '<p style="color:red;"></p>';
}
$conn->close();
?>

And in that one empty field (on the first screenshot) this code displays only "1", which is the planet owner ID (player) in the table "planets" in a row called "planets_ownerid". I want to take that "1" and use it to search in the second table called "users" in the row called "users_id" same ID number and I want to take username (Leopold in this case) in same line from the row called "users_username".

 

How can I make it? This is how my "users" table, and "planets" table looks like.

Edited by Shredon
  • Solution
9 minutes ago, Shredon said:

I want to take that "1" and use it to search in the second table called "users" in the row called "users_id" and I want to take username (Leopold in this case) in same line from the row called "users_username".

No, you really don't want to do that - use a single query which joins the two tables on the user_id.

SELECT users_username
FROM users u 
     JOIN
     planets p ON u.users_id = p.planets_ownerid
WHERE planets_galaxy = $testowanaGalaktyka 
      AND planets_starsystem = $testowanySystem 
     AND planets_planet = $testowanaPlaneta";

 

First and foremost, NEVER EVER use plaintext passwords. You need to use password_hash and password_verify.
Second, you need to use Prepared Statements. Never ever put variables in your query.

This tutorial should get you going in the right direction
https://phpdelusions.net/pdo

Barand  thank you sooo much, finally I can continue :D.  But I'm curious why there's "u" and "p". I was trying to understand that JOIN, but I didnt get that.

benanamen  I know I know, for now Im just testing that. Thank you for warning and tutorial, for sure I'll learn that :)

the u and p are alias names. they save a bunch of typing when writing queries. technically, it's AS u and AS p, but the AS keyword is optional. you must prepend the table name or its alias when a column reference is ambiguous. i personally always prepend the alias so that anyone looking at a query will know which columns are part of which tables without needing to know the table definitions.  

mac_gyver  Aaa okay, thanks for explaining :), great I know more and more. You know in my country on Polish forum about PHP only few people answered me to learn basics instead of teaching me like you all here. You're best guys, thanks. It was always my dream to make my own game in a style of OGame, since I've registered to old OGame many years ago. Now dreams became true step by step. Breathtaking hah

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.