Shredon Posted July 21, 2022 Share Posted July 21, 2022 (edited) 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 July 21, 2022 by Shredon Quote Link to comment https://forums.phpfreaks.com/topic/315073-i-cannot-get-username-from-the-second-table/ Share on other sites More sharing options...
Solution Barand Posted July 21, 2022 Solution Share Posted July 21, 2022 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"; Quote Link to comment https://forums.phpfreaks.com/topic/315073-i-cannot-get-username-from-the-second-table/#findComment-1598495 Share on other sites More sharing options...
benanamen Posted July 21, 2022 Share Posted July 21, 2022 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 directionhttps://phpdelusions.net/pdo Quote Link to comment https://forums.phpfreaks.com/topic/315073-i-cannot-get-username-from-the-second-table/#findComment-1598496 Share on other sites More sharing options...
Shredon Posted July 21, 2022 Author Share Posted July 21, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/315073-i-cannot-get-username-from-the-second-table/#findComment-1598497 Share on other sites More sharing options...
mac_gyver Posted July 21, 2022 Share Posted July 21, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315073-i-cannot-get-username-from-the-second-table/#findComment-1598499 Share on other sites More sharing options...
Shredon Posted July 22, 2022 Author Share Posted July 22, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/315073-i-cannot-get-username-from-the-second-table/#findComment-1598501 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.