Forum Moderators: coopster

Message Too Old, No Replies

Help with mysql query!

How to select multiple values from a variable

         

nfs2

12:46 pm on May 22, 2006 (gmt 0)

10+ Year Member



So i make one query to get values from one table

$result = mysql_query("SELECT entry_id FROM journal_tags WHERE journal_id = '$owner' && tag = '$tag'");

while ($row = mysql_fetch_array($result)) {
echo ''.$row['entry_id'].' ';
}

So say this query returns 4 numbers.. 111 222 333 444

Now i want to select those numbers from another table, so what i tried (and failed) to do was put those into a variable like so;

while ($row = mysql_fetch_array($result)) {
$entry_id = $row['entry_id'];
}

So now i want to make a mysql query to select those numbers from another table

$result=mysql_query("SELECT * FROM journal_entries WHERE entry_id = '$entry_id' ORDER BY entry_id DESC");

The problem is, it only returns the latest single result, not all 4.

Any idea how i can get this to work? Im kinda stuck..

dreamcatcher

1:12 pm on May 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi nfs2,

Change this:


while ($row = mysql_fetch_array($result)) {
$entry_id = $row['entry_id'];
}

to this:


$entry_id = array();
while ($row = mysql_fetch_array($result)) {
$entry_id[] = $row['entry_id'];
}

Then your new query will be:


$ids = implode(",", $entry_id);
$result=mysql_query("SELECT * FROM journal_entries WHERE entry_id IN ($ids) ORDER BY entry_id DESC");

dc

nfs2

1:45 pm on May 22, 2006 (gmt 0)

10+ Year Member



Awesome! That worked perfect

Thanks a lot