Jump to content

Using result from "select" as a variable


forTheDogs

Recommended Posts

I am trying to do a view page that will use the result of a select query page. 

 

I want to do avariables that will

 

select 'field1 value' from table where 'field value2' is equal to the selected value.

 

How do I call for the value of a field that is not known yet?

 

Is this one of those cases where one uses 'sessions'?

 

Samples would be very appreciated.  I am just starting to learn PHP.

Link to comment
https://forums.phpfreaks.com/topic/52896-using-result-from-select-as-a-variable/
Share on other sites

Thanks so much for your reply!!

 

How would I refer to the item just selected?

 

I am trying to generate a pedigree.  I have a search page where one clicks to pick which dog they want to do the pedigree for. 

 

It brings up a view page on which the pedigree will be generated.  The dog just selected will be at the top of the view page and I am trying to do variables that will select its various ancestors from the table.

 

sire=select 'sire' from table where 'just selected dog' =  'name'

 

dam=select 'dam' from table where 'just selected dog' = 'name'

 

psire=select 'sire' from table where 'sire'='name'

 

msire=select 'sire' from table where 'sire'='name'

 

pdam=select 'dam' from table where 'dam' = 'name'

 

mdam=select 'dam' from table where 'dam' = 'name'

 

and so on.

 

I am an old dog trying to learn a new trick and really appreciate any help or samples you can provide!!!

 

 

 

 

 

 

mysql> SELECT * FROM dogs;
+-----+---------+-----------+----------+
| id  | name    | sire      | dam      |
+-----+---------+-----------+----------+
| 002 | Fido    | Spot      | Pucker   |
| 003 | Spot    | Buckle    | Daisy    |
| 004 | Pucker  | Bounder   | Sissy    |
| 005 | Buckle  | Burt      | Smuckers |
| 006 | Bounder | Sprinkles | Butter   |
| 007 | Pepper  | Doggles   | Nutter   |
| 008 | Spackle | Sprinkles | Sport    |
| 009 | Daisy   | Meathead  | Sport    |
+-----+---------+-----------+----------+
8 rows in set (0.00 sec)

mysql>  SELECT a.name,a.sire,a.dam,s.sire AS psire,s.dam AS pdam,d.sire AS msire,
d.dam AS mdam FROM dogs AS a LEFT JOIN dogs AS s ON a.sire=s.name LEFT JOIN
dogs AS d ON a.dam=d.name WHERE a.name='Fido';
+------+------+--------+--------+-------+---------+-------+
| name | sire | dam    | psire  | pdam  | msire   | mdam  |
+------+------+--------+--------+-------+---------+-------+
| Fido | Spot | Pucker | Buckle | Daisy | Bounder | Sissy |
+------+------+--------+--------+-------+---------+-------+
1 row in set (0.00 sec)

mysql>  SELECT a.name,a.sire,a.dam,s.sire AS psire,s.dam AS pdam,d.sire AS msire,
d.dam AS mdam FROM dogs AS a LEFT JOIN dogs AS s ON a.sire=s.name LEFT JOIN
dogs AS d ON a.dam=d.name WHERE a.name='Bounder';
+---------+-----------+--------+-------+------+-------+------+
| name    | sire      | dam    | psire | pdam | msire | mdam |
+---------+-----------+--------+-------+------+-------+------+
| Bounder | Sprinkles | Butter | NULL  | NULL | NULL  | NULL |
+---------+-----------+--------+-------+------+-------+------+
1 row in set (0.00 sec)

 

To continue, I would just repeat the query with each of the values returned by psire...mdam until either hitting a predefined limit or NULL values, storing all values in a data structure, and then formatting them for output.

 

(Btw, I hope you're using unique, numeric ids for each dog and each other dog referenced in the table in case of same names.  I used all names just for ease of reading/example.)

Archived

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

×
×
  • 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.