How to add a second query as a condition? - php

Please see my query below. Right now its look for system_id from contacts where system_id = '$sid' but i want the query to ALSO look for friend_id from contacts where system_id = '$sid'
$tre = mysql_query("SELECT System_id, Full_name FROM accounts
WHERE Full_name LIKE '". mysql_real_escape_string($_GET['q'])."%' LIMIT 5");
while($re=mysql_fetch_array($tre))
{
$qry=mysql_query("SELECT System_id FROM contacts WHERE
System_id='". $sid ."' AND Friend_id='". $re['System_id'] ."'");
if(mysql_num_rows($qry)>0){
if($sid!=$re['user_id']){
$obx['id']=$re['System_id'];
$obx['name']=$re['Full_name'];
$arr[]=$obx;
}
}
How do i tweak it?
Thanks!
Update:
Upon looking up some of the comments below i structured this query that works as expected:
SELECT DISTINCT contacts.friend_id, accounts.full_name,
accounts.system_id
FROM contacts, accounts
WHERE (contacts.system_id = '$sid' AND contacts.friend_id
= accounts.system_id) OR (contacts.friend_id = '$sid'
AND contacts.system_id = accounts.system_id)
The only problem is how do i change the SELECT query from this:
$tre = mysql_query("SELECT System_id, Full_name FROM accounts
WHERE Full_name LIKE '". mysql_real_escape_string($_GET['q'])."%' LIMIT 5");
To $tre:
SELECT DISTINCT contacts.friend_id, accounts.full_name,
accounts.system_id
FROM contacts, accounts
WHERE (contacts.system_id = '$sid' AND contacts.friend_id
= accounts.system_id) OR (contacts.friend_id = '$sid'
AND contacts.system_id = accounts.system_id)

Related

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

How can I summarize total amount

How can I summarize total amount of users earning from a column "price" values?
$statement = "SELECT user_id, SUM(price) FROM tbl_post WHERE user_id= ".'$_SESSION['user']';
$statement = mysql_query($statement);
$user_earning= mysql_fetch_row($statement);
echo $user_earning;
you should add GROUP BY clause at select statment
$statement = "SELECT user_id, SUM(price)
FROM tbl_post
WHERE user_id= ".'$_SESSION['user']". "
GROUP BY user_id";
write above query instead of this query
$statement = "SELECT user_id, SUM(price) FROM tbl_post WHERE user_id= ".'$_SESSION['user']';
i dont know about php but by see your query you want to sum of all price of a perticular user.
Try this, you have error in your query :
$statement = "SELECT user_id, SUM(price) FROM tbl_post WHERE user_id= '".$_SESSION['user']."' ";
$statement = mysql_query($statement);
$user_earning= mysql_fetch_row($statement);
echo $user_earning[1];// echo $user_earning['price'];
Your SQL query should look like that:
select user_id,sum(price) from tbl_post where user_id=? group by user_id;

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.

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

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.

Categories