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

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.

Related

How to get all the names of the tables involved in a sql query?

For example:
I have this query:
SELECT first_name
FROM users
INNER JOIN roles ON roles.id = users.id_roles
WHERE roles.name = 'admin';
I need an Array with the name of the tables used in the query,like that:
['users','roles'];
I think this would be tough using regex - unless you know your queries are all consistently following the same standard. Here's an option using EXPLAIN.
$tables = [];
$query = "EXPLAIN SELECT first_name FROM users INNER JOIN roles ON roles.id = users.id_rolesWHERE roles.name = 'admin'";
$q = mysqli_query($link, $query);
while($r = mysqli_fetch_assoc($q)) {
$tables[] = $r['table'];
}
print_r(array_unique($tables));

Cannot store SELECT query into PHP variable

I'm trying to store a query result in order to use it in another SELECT statement but it isn't working..
$username = $_SESSION['username'];
$result = "SELECT sensorid FROM users WHERE username = '$username' ";
$result is supposed to have an integer but how can I use that variable into another select like...
$sql = "SELECT * FROM sensor WHERE sensorid = '$result'";
You need to join users table with sensor table on sensorid column.
$query = "select s.* from users u join sensor s on s.id = u.sensorid where u.username = $username"
See this

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 to search a word on two table if have or not by php query

I want to find out my userid from two table by username where username have already stayed in table2 or not. That means if username match in any table my query collect its userid.
table 1: username --- userid > john --- 100
table 2: username --- userid > colin --- 101
I read many article here nut I cannot understand what should I do.
Now think: I need john's uesrid,
So I tried:
$username = "john";
$q = "SELECT userid FROM table1,table2 WHERE table1.username = '$username' or table2.username = '$username'";
$result = mysqli_query($this->connection, $q);
From what i understand you want to check if the name already exist in one of your tables.
Here is the easy php code:
<?php
session_start();
include 'db_connect.php';
$check_1 = mysqli_query("SELECT * FROM Table_1 WHERE Username = '$Username'");
$check_2 = mysqli_query("SELECT * FROM Table_2 WHERE Username = '$Username'");
if(mysqli_num_rows($check_1)==0){
//Didnt exist in table 1
}
else if(mysqli_num_rows($check_2)==0){
//No result in table 2 either.
}
else{
//what you want to do if the username doesnt exist
}
Let me know how it worked out for you :)
You could select both userid fields and afterwards look if one of them is set:
$username = "john";
$q = "SELECT table1.userid AS userid1,table2.userid AS userid2 FROM table1,table2 WHERE table1.username = '$username' or table2.username = '$username'";
$result = mysqli_query($this->connection, $q);
Try this:
$username = "john";
$q = "SELECT table1.userid FROM table1 JOIN table2 ON table1.username=table2.username WHERE table1.username = '$username'";
$result = mysqli_query($this->connection, $q);
JOIN will join table2 to table1 if the maching pattern (table1.username=table2.username) equals true or exclude this row from table1 from results.
You can use UNION clause
$q="SELECT t1.userid FROM table1 as t1 WHERE username='$username' UNION SELECT t2.userid FROM table2 as t2 WHERE username='$username'";
You can use left join
$username = "username";
$q = "SELECT userid
FROM table1
LEFT JOIN table2 ON table1.username = table2.username
WHERE table1.username = '$username' or table2.username = '$username'";
$result = mysqli_query($this->connection, $q);

One variable row inside another variable row

I have following script:
$sql = "SELECT * FROM `users`"
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = "SELECT * FROM `other_table`";
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
$item = $row1[$row['username']];
How can I set one variable row inside another, since it does not work. Basically, I need to select username, and then select column with user username from other table, in which is written user points.
I was thinking about adding:
$sql = "SELECT * FROM `users`"
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = "SELECT `".$row['username']."` FROM `other_table` WHERE `uid` = 1";
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
$item = $row1[xxxxxxxxxx]; // DONT KNOW HOW TO DEFINE IT, so it takes out found variable (there is only one).
Guess you want something like
SELECT * FROM table1 t1, table2 t2 WHERE t1.user_name = t2.user_name?
Think about using JOIN
$sql = "SELECT * FROM users;";
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = sprintf("SELECT * FROM other_table where username='%s';", $row['username']);
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
// now $row1 contains the tuple of this user and could access the variables are you would
// normally do e.g. $row1['ID']
SELECT * FROM users AS u INNER JOIN other_table AS o ON u.username = o.username
I'm assuming you want to do this because you want to be able to access all the rows from either table where a particular user name is the same (e.g. the data from users where username="john" and the data from other_table where username="john" for all usernames). No need to nest a result set to do this, just use a JOIN statement and then you can access all the columns as if it was a single result set (because it is):
$sql = "SELECT * FROM users AS u INNER JOIN other_table AS o ON u.username = o.username";
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$item = $row['any_column'];
FYI you should list out the column you want to retrieve instead of using *, even if you want to retrieve them all, as it is better practice in case you add new columns in the future.

Categories