Cannot store SELECT query into PHP variable - php

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

Related

how to create double select query

<?
session_start();
$username = $_SESSION["username"];
$lecturername = "SELECT lecturername FROM lecturer WHERE username='$username'";
$sql = "SELECT * FROM publication WHERE lecturername='$lecturername'";
$records = mysql_query($sql);
?>
Try using joins like this (just an example):
SELECT * FROM lecturer t1 JOIN publication t2 ON (t1.lecturername = t2.lecturername) WHERE username='$username'
This will return data of the combined tables lecturer and publication, using username to filter the results from lecturer table, and therefore publications.

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);

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.

How to use php variables in mysql query while php variable contains mysql query?

How can I implement something like this in mysql?
$query1 = "SELECT id FROM table WHERE username = 'John'";
$query2 = "SELECT id FROM table WHERE username= 'Parsa'";
$query = "SELECT * FROM table WHERE id BETWEEN $query1 AND $query2";
$result = mysql_query($query) or die('Query faild'.mysql_error());
$myrecord = mysql_fetch_assoc($result);
Try this
$query1 ="SELECT GROUP_CONCAT(id) FROM table WHERE firstname in('John','Parsa')";
$query = "SELECT * FROM table WHERE id IN ($query1)";
you have two identical queries , you could just have one . and use IN , not BETWEEN.
You can put those 3 queries in to one query:
$query = "SELECT * FROM table WHERE id
BETWEEN
( SELECT id FROM table WHERE firstname = 'John' GROUP BY id )
AND
( SELECT id FROM table WHERE firstname = 'Parsa' GROUP BY id )
";
although your query doesn't mean anything; you need "()" for subqueries to work.
$query1 = "(SELECT id FROM table WHERE username = 'John')";
$query2 = "(SELECT id FROM table WHERE username= 'Parsa')";
$query = "SELECT * FROM table WHERE id BETWEEN $query1 AND $query2";
u can use a subselection:
SELECT * FROM table WHERE id BETWEEN ($query1) AND ($query2)
But be careful: The Subselection result must be an Integer.

Select all values from multiple tables

I am new to both mysql and php.
I have two tables which are 'members' and 'points'. Both of them including the column 'username'. I want to select all the values from these two tables where username= $POST[username].
So I wrote this but this is not working.
$username = $_POST['username'];
$sql = $con->prepare("SELECT *.members, *.points FROM members, points WHERE
username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
And I tried this :
$sql = $con->prepare("SELECT * FROM members INNER JOIN points
ON username.points = username.members WHERE username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
And this:
$sql = $con->prepare("SELECT *.points, *.members FROM members INNER JOIN points
ON username.points = username.members WHERE username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
I can't use UNION because the number of columbs are not equel in these tables.
So, Please help me what is wrong with the code? What is the proper way to select all from multiple tables.
Alias are meant to be used to specify to which table those column belong, so you need to prepend table name to your columns
SELECT * FROM members
INNER JOIN points
ON points.username = members.username
WHERE points.username = ?
You can otherwise assign an alias to your table while selecting and use them
SELECT * FROM members a
INNER JOIN points b
ON a.username = b.username
WHERE a.username = ?
You were close with this:
SELECT *.points, *.members
FROM members
INNER JOIN points ON username.points = username.members
WHERE username=?
Try this instead:
SELECT *
FROM members
INNER JOIN points ON members.username = points.username
WHERE members.username=?
check this
SELECT * FROM points,members WHERE points.username="'.$_POST['username'].'" AND members.username="'.$_POST['username'].'";
you can check this query it is very simple.

Categories