Jump to content

Archived

This topic is now archived and is closed to further replies.

salto

Getting a variable out of an array (from query) and using it for another query?

Recommended Posts

[b]EDITED by ryanlwh: removed the connection info for security reason stated by Ken[/b]

I am a relative newbie, so I apologize for my ignorance.  I am using the form handler below to query a single table and spit back a table of results.

[code]<!-- Cedula Query Form Handler -->
<?php
$cedula2 = $_POST['cedula'];
$canton2 = $_POST['canton'];
$link = mysql_connect('****', '****', '****');
if (!$link) {
  die('Not connected : ' . mysql_error());
}

// make canton2 the current db
$db_selected = mysql_select_db($canton2, $link);
if (!$db_selected) {
  die ('Can\'t connect to $canton2 : ' . mysql_error());
}
?>
<?php
$query = mysql_fetch_array(mysql_query("SELECT * FROM informacion_registral WHERE CEDULA_PERSONAL LIKE '$cedula2'"), MYSQL_ASSOC);
echo "<table cellpadding=1 cellspacing=1 border=1>\n";
foreach ($query as $key => $value) {
  echo "<tr><td>$key</td><td>$value</td></tr>\n";
}
echo "</table>\n";
?>[/code]

The table it spits out looks like this.  When I enter 105710879 as the Cedula in the first form.

<!-- Cedula Query Form Handler -->
<table cellpadding=1 cellspacing=1 border=1>
<tr><td>ID</td><td>6</td></tr>
<tr><td>REC_NO</td><td>6</td></tr>
<tr><td>CATASTRO_LINC</td><td>64302</td></tr>
<tr><td>PROPIETARIO</td><td>xxxxxxx</td></tr>
<tr><td>CEDULA_PERSONAL</td><td>105710879</td></tr>
<tr><td>FOLIO_REAL</td><td>230810</td></tr>
<tr><td>PLANO_CATASTRO</td><td>xxxxxxx</td></tr>
<tr><td>FECHA_INSCRIPCION</td><td>xxxxx</td></tr>
<tr><td>AREA_REGISTRO</td><td>217.58</td></tr>
<tr><td>NATURALEZA</td><td></td></tr>
<tr><td>GRAVAMENES</td><td></td></tr>
<tr><td>ANOTACIONES</td><td></td></tr>
<tr><td>OBSERVACIONES</td><td> </td></tr>
</table>

What I would like to do is use the value for CATASTRO_LINC (in this case 64302) in another query.  Is there a call of some kind which will pull the catastrol_linc value out of that array and make it a variable so that I can use it to query a second related table and display its records as well?

Ideally I would like to spit it back into another query and generate another table underneath.

[code]
...
<?php
$query = mysql_fetch_array(mysql_query("SELECT * FROM TABLE2 WHERE CATASTRO_LINC LIKE '$the variable we created from the array'"), MYSQL_ASSOC);
echo "<table cellpadding=1 cellspacing=1 border=1>\n";
foreach ($query as $key => $value) {
  echo "<tr><td>$key</td><td>$value</td></tr>\n";
}
echo "</table>\n";
?>[/code]

I hope this makes sense, and thank you for any insite on how to do this.

Cheers.
Spencer

Share this post


Link to post
Share on other sites
in your foreach looop
[code]<?php
foreach ($query as $key => $value) {
  $$key = $value; //create variables like $ID, $REC_NO, etc..
  echo "<tr><td>$key</td><td>$value</td></tr>\n";
}
?>[/code]

notice the two $

Share this post


Link to post
Share on other sites
What does the output of the array look like? Just to be clear...is the output spitting back out the values...or the HTML as well? And am I to understand you would like to retrieve one of the values embedded in the HTML? Maybe I'm not reading your post correctly....long day.

Share this post


Link to post
Share on other sites
The output of the array is just the values I think and the html is added in by the tables tag in this part of the function.  The $query variable is the results of the array to my limited understanding.

[code]echo "<table cellpadding=1 cellspacing=1 border=1>\n";
  echo "<tr><td>$key</td><td>$value</td></tr>\n";
[/code]

The post by ryanlwh is on to something although I haven't figured it out yet.  Creating a $CADASTRO_LINC variable from the initial query and using it again is exactly what I need to do.  I'm working on wrapping my head around ryanlwh's response... 

Thanks again!
Spencer

Share this post


Link to post
Share on other sites
try the $$key=$value thing i posted, then use $CADASTRO_LINC in the later part of your script. you'll find that this variable is created, along with $ID, $REC_NO, etc.

Share this post


Link to post
Share on other sites
Please edit your original post and remove any usernames, passwords, and domains from your mysql functions. Also you better change you mysql password NOW or some unscrupulous person may corrupt your information.

[b]NEVER[/b] post usernames or passwords to a public forum like this.

Ken

Share this post


Link to post
Share on other sites
I've edited the post for you salto. Thanks Ken for pointing it out. I didn't even notice he posted the info.

Share this post


Link to post
Share on other sites
Why not put the first output in a session then use that session on the 2nd database query.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.