Can php request results from a previous query?

In some languages ​​(ColdFusion comes to mind), you can run a query from a result set from a previous query. Is it possible to do something like this in php (with MySQL as the database)?

I kind of want to:

$rs1 = do_query( "SELECT * FROM animals WHERE type = 'fish'" ); $rs2 = do_query( "SELECT * FROM rs1 WHERE name = 'trout'" ); 
+7
php mysql search resultset
source share
5 answers

For PHP there is no such MySQL function, but for it there is a more advanced one.

Edit: For those of you who don’t know what a query request is, this is precisely the goal that some people do. Using the AND operator is **** NOT **** the same! If I need results where username = 'animuson' for one part of my script, and then you want all the results to be from this query, where status = '1', it is not logical for me to run another query using the AND operator, it is much more logical iterate over previous results in PHP. Stop stopping things without reading comments about why they weren't supported in the first place, it's just lazy. If you do not know what they are talking about, you should not raise or subvert in the first place.

+6
source share

Well, you can do this without touching db:

 while($t = mysql_fetch_array($rs1)){ if($t[name] == 'trout'){ echo 'This is the one we\'re looking for!'; break; } } 
+2
source share

In PHP, this would be terribly inefficient. You will need to go through each line and verify that its name was trout . However, are there any reasons why you cannot do

 SELECT * FROM `animals` WHERE `type` = 'fish' AND `name` = 'trout' 

in SQL? That would be much, much faster.

+1
source share

You can also do something like

 select morestuff from (select stuff from table where a = b ) where c = d; 
+1
source share

Use the AND keyword?

 "SELECT * FROM animals WHERE type = 'fish' and name='trout'" 

Alternatively, you can use LINQ for php http://phplinq.codeplex.com/

0
source share

All Articles