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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.