Jump to content

Using result from one SQL statement in new SQL statement


damo87

Recommended Posts

I have an sql statement that returns a list of user ids from one database.  I then need to run some SQL on a different database for each of those user ids.

 

The tricky thing for me here is that they are two different databases, so I assume I can't get all the data I need in one SQL statement.

 

I really have no idea how to start on this, so at this stage even a push in the right direction, ie. a description of generally what I need to do/what to google on, would be helpful.  Thanks!

OK, I think I've sorted this one out.

 

The output of the first SQL statement results in this array:

 

Array ( [0] => Array ( [uid] => 1234 ) [1] => Array ( [uid] => 4321 ) )

 

So I need to turn the array values into comma separated values, and remove the last comma (note - because it is a multidimensional array, the implode function will not work):

 

$uid = '';
foreach($resultarray as $v1) {
$uid .= $v1["uid"] . "," ;
}
$uid=substr($uid, 0, -2);

 

Then insert into the next statement:

$query = "select * from user where userid IN($uid)";
$result = mysql_query($query) or die("Query failed : " . mysql_error());

 

Which gives me an array with the data I need.

 

 

You could also simply use....

 

$query = "select * from user where userid IN(" . implode(',', $resultarray) . ")";

 

Yes, if it wasn't a multi dimensional array.  Imploding my array just gives me the word 'array' rather than the user id's I need.

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.