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!

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

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.