SQL - How do i use a selected ID in another table? - php

SQL TABLES:
user = ID, name, email..
hero = ID, name, level..
owner = userid, heroid
I have these two queries:
$result = mysql_query("SELECT heroid FROM owner WHERE userid='$id'");
$heroid=mysql_fetch_row($result);
$result2 = mysql_query("SELECT name FROM hero WHERE ID = 'heroid[0]'");
$heroname = mysql_fetch_row($result2);
echo "User $id has a character named $heroname[0]";
$heroname = mysql_fetch_row($result2);
echo "User $id has a character named $heroname[0]";
..
Can this be done with a single query?

Yes. you can do:
SELECT h.name
FROM hero h
INNER JOIN owner o ON o.heroid = h.id
WHERE o.userid = $id

Related

Converting SQL Table ID into another field in PHP

So I'm trying to get the following to work;
//SEARCH USERPETS TABLE FOR ANYTHING USER OWNS
$query = "SELECT * FROM userpets WHERE owner = '$username'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$userspets = $row['petid'];
//SEARCH PETS TABLE FOR LIST OF PET NAMES AND DETAILS
$query = "SELECT * FROM pets";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
$petid = $row['id']
$petname = $row['petname'];
$petimg = $row['petimg']
//TURN PET ID INTO PET NAME AND IMAGE
echo "Pets: ".$userspets;
Essentially what I'm trying to do is this;
The 'userpets' table contains all 'owned' pets and the players username is displayed.
I want to grab all pets owned by that user and compare the petid with the 'pets' table.
I then want to take the pet's name and image from that table and 'echo' it onto the page.
Getting all the ids is fine, I just don't know how to make it convert the id's into the names.
Table Structure
You can use JOIN of MYSQL or Foreach of PHP
This is example by using PHP Foreach
$query = "SELECT * FROM userpets WHERE owner = '".$username."'";
$result = mysqli_query($conn, $query);
$petid = array(); // store all petid of this user
$rows = mysqli_fetch_all($result,MYSQLI_ASSOC);
foreach($rows as $row) {
$petid[] = $row['petid'];
}
$query = "SELECT * FROM pets WHERE id IN (".implode(",",$petid).")";
// implode will convert an array to string with delimete
// example array(0=>35, 1=>36, 2=>48) will be convert to "35,36,48"
// and above query should be : SELECT * FROM pets WHERE id IN (35,36,48)
$result = mysqli_query($conn, $query);
$pets = mysqli_fetch_assoc($result);
// dump it
echo "<pre>";
var_dump($pets);
echo "</pre>";
die;
Using MySQL Join
<?php
$query = "SELECT pet.id, pet.petname, pet.petimg, up.owner FROM pets as pet LEFT JOIN userpets as up ON pet.id = up.pet_id WHERE up.owner = '".$username."'";
I don't know how your tables look, but the best thing I can think of is, you have 3 tables one with Users, second with pets, and third "many to many" table lets call it ownedpets with users that own pets, because many users can own many pets. So ownedpets should have id_users that is connected to user.id and id_pets which is connected to pets_id. With that in mind I would do the fallowing query
SELECT *
FROM ownedpets
LEFT JOIN users
ON users.id = ownedpets.id_users
LEFT JOIN pets
ON pets.id = ownedpets.id_pets
WHERE users.id = $user_id
hope this helps

How can I turn these multiple mysql_querys into 1 query?

I have two tables: users and threads. When a thread is created, it will store the user_id in the table as author_id. I want to display the thread name and the username of its author with the same query. I am currently using two querys as shown:
$query2 = mysql_query("SELECT author_id FROM threads WHERE id = $threadId") or die(mysql_error());
$result2 = mysql_fetch_array($query2);
$author_id = $result2['author_id'];
$query3 = mysql_query("SELECT username FROM users WHERE id = $author_id") or die(mysql_error());
$result3 = mysql_fetch_array($query3);
$author_name = $result3['username'];
<?php
$sql = '
SELECT t.name, u.username
FROM threads t
JOIN users u ON t.author_id = u.id
WHERE t.id = ' . (int)$threadId . '
';
list($thread_name, $author_name) = mysql_fetch_row(mysql_query($sql));
P.S. Mysql php extension is deprecated.
Try this query:
SELECT username
FROM users
WHERE id = (SELECT author_id
FROM threads
WHERE id = $threadId
LIMIT 1)
Note: Limit 1 is not mandatory as id is unique.

sql select from table then compare with other table in one statement

I have to tables in one database.
users
user_activate
I have to variables in php
$username = foo;
$key = jas823k123ksd34324;
Now I want to select from the table users the attribute user_id where user_username == $username
I do this with this statement
$sql = "SELECT user_id FROM users WHERE user_username = '$username'";
$result = mysqli_query($db, $sql);
while($row = mysqli_fetch_assoc($result)){
$user_id = $row['user_id'];
}
Now I want to select from the table user_activate the attribute user_activate_key where user_activate_key == $key;
For this I use this statement:
$sql2 = "SELECT user_activate_key FROM user_activate WHERE user_activate_key = '$key'";
$result2 = mysqli_query($db, $sql2);
while($row = mysqli_fetch_assoc($result)){
$user_key = row['user_activate_key'];
}
Can I do both statements in one statement?
As you've written it, two seperate queries is the correct way to do it. But I suspect that there's some kind of relationship between users and user_activate that might make what you're asking for make sense. Assuming that a user_activate_key is tied to a specific user_id, you could do something like the following:
select users.user_id, ua.user_activate_key
from users u
left join user_activate ua
on u.user_id = ua.user_id
and ua.user_activate_key = '$key'
where u.username = '$username'
The LEFT JOIN means that the user will be shown even if there isn't a matching user_activate_key record.

MySql, php: Read from 2 Tables with same User ID

Hello Sorry for the previously Post:
I habe a table ´Nachrichten´ and a table 'user'.
I want the rows from 'Nachrichten' where Quelle is like $_SESSION["id"]. Quelle is a UserID. Now I need the rows from 'user' where ID is like $_SESSION["id"], too beacause I need the name and lastname from the user: This is my Query but I get no faild but no results too. Thanks for support.
$abfrage = "SELECT * FROM Nachrichten n JOIN user u ON u.id = n.Quelle WHERE n.Quelle LIKE '".$_SESSION["id"]."'";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis))
{
$id = $row["n"]->ID; $ziel = $row["n"]->Ziel; $uhrzeit = $row["n"]->Uhrzeit; $gelesen = $row["n"]->Gelesen; $inhalt = $row["n"]->Inhalt; $vorname = $row["u"]->Vorname; $nachname = $row["u"]->Nachname;
kontakt($ziel, $vorname, $nachname, $uhrzeit, $gelesen);
echo "hjfe";
}
Just guessing...
$abfrage = "
SELECT *
FROM Nachrichten n
JOIN user u
ON u.id = n.Quelle
WHERE n.Quelle = '{$_SESSION['id']}';
";

Looping through a mysqli result

I'm trying to display a list of status updates from artists that a logged in user is following.
So far I have this:
#Get the list of artists that the user has liked
$q = "SELECT * FROM artist_likes WHERE user_id = '1' ";
$r = mysqli_query($dbc,$q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
#Now grab the statuses for each artist
$status_query = "SELECT * FROM status_updates WHERE artist_id = '".$row['artist_id']."' ";
$status_result = mysqli_query($dbc,$status_query)
}
But i'm not sure how to loop through and display the returned status updates?
This isn't a strong point of mine, so any pointers would be greatly appreciated!
What prevented you from doing similar to what you'd already done for the first query? Something like follows:
#Get the list of artists that the user has liked
$q = "SELECT * FROM artist_likes WHERE user_id = '1' ";
$r = mysqli_query($dbc,$q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
#Now grab the statuses for each artist
$status_query = "SELECT * FROM status_updates WHERE artist_id = '".$row['artist_id']."' ";
$status_result = mysqli_query($dbc,$status_query)
while($status_result_row = mysqli_fetch_assoc($status_result)) {
echo $status_result_row['mycol']; // This is where you know better than us
}
}
Or if those two tables artist_likes and status_updates have artist_id in common then you could just use one query with a join. (But don't know if you are asking for that).
Just for avoiding multiple query, you can use one query like this:
SELECT l.*, s.*
from artist_likes l, status_updates s
WHERE
l.artist_id = s.artist_id and
l.user_id = '1'
or
SELECT l.*, s.*
from artist_likes l
JOIN status_updates s on (l.artist_id = s.artist_id)
WHERE
l.user_id = '1'

Categories