mysql - php pass value to multiple select queries -


i'm having trouble passing variable between mysql queries on same page. maybe can advise i'm doing wrong. i'm new php/mysql, answer seems easy, don't see it. here 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 list of results. reaults identified [$id] , <a href> link passes [$id] page result details. works perfectly.

3. details page. query result search results page , display related information table, identified [$id]. works fine.

    <?php      $sql = "select * table_a     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 details page, want show related data table b in same database, using "protein_id" query above. can't work, pass "protein_id" next query, follows:

table b:

    id | protein_asc | synonym | name | etc     ----------------------------------------------     11  | p12803    | abc      | |     12  | p99613    | def      | |      <?php      $new_id = $row[protein_id];      $sqla = "select * table_b     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 achieve this, using joins on second select query, nothing seems work. know second query correct, because if hard code "$new_id = p12803 ;" second query grabs data.

any appreciated.

thanks

you need move second loop inside first loop otherwise $row[protein_id] return null. furthermore assign $row both fetching, changed second $rowa. change to

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>  <?php      $new_id = $row[protein_id];      $sqla = "select * table_b     protein_asc='".$new_id."'";      $rsa_result = mysql_query ($sqla,$connect);       while ($rowa = mysql_fetch_assoc($rsa_result)) {      ?>      <table class="table">     <tr><td>synonym: </td><td><? echo $rowa[synonym]; ?></td></tr>     <tr><td>name: </td><td><? echo $rowa[name]; ?></td></tr>     </table>      <?  } ?>  <?  } ?> 

then remember mysql_ functions deprecated advise switch mysqli or pdo , indeed @ risk of sql injection, have here how can prevent sql injection in php?. should use prepared statment avoid risk


Comments

Popular posts from this blog

Socket.connect doesn't throw exception in Android -

SPSS keyboard combination alters encoding -

iphone - How do I keep MDScrollView from truncating my row headers and making my cells look bad? -