Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Array in PHP/MySQL | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
Mercury

Shyguy
Level: 16

Posts: 55/88
EXP: 18132
For next: 2124

Since: 07-08-04
From: Hihihi.

Since last post: 1 day
Last activity: 1 day
Posted on 08-07-05 09:37 PM Link | Quote
Let's say I have this array, which contains id's from a certain table in the DB:
$testarray = array( "5", "28", "62", "39", "55", "72" );


Now what I wanna do is, create a new array, using data from the database where the id's are like in the testarray. So something like this (doesn't work naturally):


$newarray[]=mysql_fetch_array(mysql_query("SELECT fields FROM table WHERE id IN ('$testarray[]')"));

So this query should store all the fields (which have the id's from the testarray) in the newarray, so I could do for example:

print "$newarray[1]";

(this array element should be the info which has id 28 in that specific table)



Dunno if this is clear, but in any case, what is the right query to do this (the one in bold)?
neotransotaku

Baby Mario
戻れたら、
誰も気が付く
Level: 87

Posts: 3774/4016
EXP: 6220548
For next: 172226

Since: 03-15-04
From: Outside of Time/Space

Since last post: 11 hours
Last activity: 1 hour
Posted on 08-08-05 01:35 AM Link | Quote
To get all the fields, your query would be

SELECT * FROM table WHERE id IN (5, 28, 62, 39, 55, 72)

that query assumes your ids in your database tables is actually numbers and not strings. That same query may work for strings if you quote each number. If not, then just iterate through each id with the following query. For example, 5:

SELECT * FROM table WHERE id = 5 // int representation
SELECT * FROM table WHERE id = "5" // string representation
Mercury

Shyguy
Level: 16

Posts: 56/88
EXP: 18132
For next: 2124

Since: 07-08-04
From: Hihihi.

Since last post: 1 day
Last activity: 1 day
Posted on 08-09-05 01:02 AM Link | Quote
I dropped the 'IN' argument, it wouldn't really have worked with the array anyway.

I fixed it on some other way using a loop .

$game=mysql_fetch_array(mysql_query("SELECT scards FROM games WHERE id=4"));
$sc=unserialize($game[scards]);

$imgsc = array( "0", "0", "0", "0", "0", "0" );
$i=0;
while ($i < 6) {
$cimg=mysql_fetch_array(mysql_query("SELECT url FROM cards WHERE id = $sc[$i]"));
$imgsc[$i] = $cimg[url];
$i++;
}
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - Array in PHP/MySQL | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.011 seconds.