I'm having trouble passing a variable between MySQL queries on the same page. Maybe someone can advise what I'm doing wrong. I'm new to PHP/MySQL, but the answer seems very easy, I just don't see it. Here is what I have:
1. MySQL: Table A:
id | gene_id | protein_id | disease_id | etc
----------------------------------------------
1 | 672 | P12803 | 091312
2 | 817 | P99613 | 020346
3 | 411 | P52021 | 055823
2. Search result page. Displays a list of results. The reaults are identified by [$id] and an <a href>
link passes [$id] to another page for result details. This works perfectly.
3. Details Page. I get the query result from the Search results page and display related information from the table, identified by [$id]. This works fine.
<?php
$sql = "SELECT * FROM Table_A
WHERE id=" . $_GET["id"];
$rs_result = mysql_query ($sql,$connect);
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<table class="table">
<tr><td>Gene: </td><td><? echo $row[gene_id]; ?></td></tr>
<tr><td>Protein: </td><td><? echo $row[protein_id]; ?></td></tr>
<tr><td>Disease: </td><td><? echo $row[disease_id]; ?></td></tr>
</table>
<? } ?>
4. Show related data. On the details page, I want to show related data from Table B in the same database, using the "protein_id" from the query above. But this I can't get to work, to pass "protein_id" to the next query, as follows:
Table B:
id | protein_asc | synonym | name | etc
----------------------------------------------
11 | P12803 | ABC | this |
12 | P99613 | DEF | that |
<?php
$new_id = $row[protein_id];
$sqla = "SELECT * FROM Table_B
WHERE protein_asc='".$new_id."'";
$rsa_result = mysql_query ($sqla,$connect);
while ($row = mysql_fetch_assoc($rsa_result)) {
?>
<table class="table">
<tr><td>Synonym: </td><td><? echo $row[synonym]; ?></td></tr>
<tr><td>Name: </td><td><? echo $row[name]; ?></td></tr>
</table>
<? } ?>
I have tried many different ways to achieve this, using joins on the second Select query, but nothing seems to work. I know the second query is correct, because if I hard code "$new_id = P12803 ;" then the second query grabs the data.
Any help would be appreciated.
Thanks