0

I'm trying to combine two queries.

$query_1 = mysql_query("SELECT * FROM `table1_results` WHERE id=".$_GET['id']);
while($table1_results = mysql_fetch_assoc($query_1)){
    $total[] = $table1_results;
}

and

$query_1 = mysql_query("SELECT * FROM `table2_results` WHERE id=".$_GET['id']);
while($table2_results = mysql_fetch_assoc($query_1)){
    $total[] = $table2_results;
}

everything remains the same, just I want to combine two different tables

table2_results

and

table2_results in one query

I consulted a lot of examples, but did not get the desired result, please give an example proper solution to the problem thank you

Martin
  • 22,212
  • 11
  • 70
  • 132
Aleza
  • 15
  • 4
  • do they have any relation to each other – Aman Rawat Apr 27 '16 at 18:59
  • @AmanRawat It must be a query that joins two different tables – Aleza Apr 27 '16 at 19:01
  • 1
    While you're at it, make sure you fix that SQL-injection vulnerability: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Peter van der Wal Apr 27 '16 at 19:04
  • **WARNING**: If you're just learning PHP, please, do not learn the obsolete [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It's awful and has been removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) helps explain best practices. Your user parameters are **not** [properly escaped](http://bobby-tables.com/php) and this has [SQL injection bugs](http://bobby-tables.com/) that can be exploited. – tadman Apr 27 '16 at 19:44

2 Answers2

2

You should use an inner join

  SELECT * FROM `table1_results`  as a
  Inner join `table2_results` as b on a.id = b.id 
  WHERE a.id=".$_GET['id']
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

How about select multiple tables?

Select * from `table1_results` as t1, `table2_results` as t2
where t1.id = $_GET['id'] OR t2.id = $_GET['id']
Ethan F.
  • 251
  • 1
  • 7