0

can please someone help me? I do the work at school and I dont know how connect these two tables.

I have two tables :

|Analyze                  | | Data
+-------------------------+ +-------------------------+
| id_analyze | Name       | | id_analyze | Standard   |  
---------------------------  -------------------------
| 1          | One          | 1          | A.11       |
| 2          | two          | 1          | A.12       |
| 3          | tree         | 1          | A.13       |
| 4          | four         | 2          | A.21       |
| 5          | five         | 2          | A.22       |
| id_analyze | Name         | 3          | A.31       |      

All, what I need is get output on web page from these two tables like:

| One  | A.11 |
|      | A.12 |
|      | A.13 |
---------------
| Two  | A.21 |
|      | A.22 |

I tried a lots of listing but nothing work. For example:

$choose = mysql_query("select * FROM Analyze", $connection);
$choose2 = mysql_query("select * FROM Data,Analyze WHERE Data.id_analyze = Analyze.id_analyze", $connection);

while ($row = mysql_fetch_array( $choose ) ){   
    while ($row2 = mysql_fetch_array( $choose2 )) {
        $Name = $row['Name'];
        $Standard = $row2['Standard'];
    }
}
wogsland
  • 9,106
  • 19
  • 57
  • 93
Peter Valek
  • 87
  • 1
  • 2
  • 11
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 22 '16 at 16:10

1 Answers1

0

Simple JOIN query:-

SELECT Analyze.Name, Data.Standard 
FROM Analyze 
INNER JOIN Data
ON Data.id_analyze = Analyze.id_analyze
ORDER BY Analyze.Name, Data.Standard 

Note that you can do joins how you have done them but the newer way is explicitly using INNER JOIN / LEFT OUTER JOIN / etc.

You can then just loop around the results of this once

Kickstart
  • 21,403
  • 2
  • 21
  • 33