Jump to content

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


salto

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
Link to comment
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.
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.