Jump to content

Selecting Multiple Rows Without Using "while" ?


yarub

Recommended Posts

I don't even know what I'm trying to do.. but I can't figure out how to do it.. or how I should do it.

 

I have a table with a bunch of rows in it.. let's say 10 rows. I want to output two fields from each one of those rows wherever I want on my page. So if I do it this way, I can't use:

 

while($result = mysql_fetch_array($query)){

 

I can't use that because that assumes I want to repeat each line. However, I don't want to use a new query each time because the actual number I'll be using is closer to 100. So if that makes sense.. does anyone have any ideas?

I was messing around with different array functions, but I couldn't find the one that does what I want it to do. I don't know which function to use that will store it. Can you point me in the right direction? My database pretty much looks like this:

 

[rowid1] [field] [value]

[rowid2] [field] [value]

[rowid3] [field] [value]

[rowid4] [field] [value]

[rowid5] [field] [value]

 

I want to be able to output the field and value from any row at any time.

Yeah, then you're going to want to do what Pikachu2000 suggested. For example:

 

$data = array();
$result = mysql_query('...');
while($row = mysql_fetch_assoc($result)) {
    $data[$row['id']] = $row;
}

 

Then you'll be able to access data later on like so:

 

echo $data[5]['some_field']; // outputs the value of 'some_field' column that corresponds to a row with the column id equal to 5

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.