forTheDogs Posted May 25, 2007 Share Posted May 25, 2007 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 More sharing options...
georg Posted May 25, 2007 Share Posted May 25, 2007 Just try to use: SELECT field1 FROM table WHERE field1=field2; There, it would be possible to create an index on field2. It'll speed up the query!! Link to comment https://forums.phpfreaks.com/topic/52896-using-result-from-select-as-a-variable/#findComment-261369 Share on other sites More sharing options...
forTheDogs Posted May 25, 2007 Author Share Posted May 25, 2007 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!!! Link to comment https://forums.phpfreaks.com/topic/52896-using-result-from-select-as-a-variable/#findComment-261569 Share on other sites More sharing options...
Wildbug Posted May 25, 2007 Share Posted May 25, 2007 http://dev.mysql.com/doc/refman/5.1/en/selecting-rows.html Link to comment https://forums.phpfreaks.com/topic/52896-using-result-from-select-as-a-variable/#findComment-261573 Share on other sites More sharing options...
forTheDogs Posted May 26, 2007 Author Share Posted May 26, 2007 Has anyone ever done a "SET_INTO"? If so do you think this would work? Link to comment https://forums.phpfreaks.com/topic/52896-using-result-from-select-as-a-variable/#findComment-262094 Share on other sites More sharing options...
bubblegum.anarchy Posted May 27, 2007 Share Posted May 27, 2007 Explain SET_INTO. Link to comment https://forums.phpfreaks.com/topic/52896-using-result-from-select-as-a-variable/#findComment-262335 Share on other sites More sharing options...
Wildbug Posted May 28, 2007 Share Posted May 28, 2007 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.) Link to comment https://forums.phpfreaks.com/topic/52896-using-result-from-select-as-a-variable/#findComment-263341 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.