yarub Posted August 17, 2010 Share Posted August 17, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/210922-selecting-multiple-rows-without-using-while/ Share on other sites More sharing options...
pudge1 Posted August 17, 2010 Share Posted August 17, 2010 What do you mean by "repeat each line" Quote Link to comment https://forums.phpfreaks.com/topic/210922-selecting-multiple-rows-without-using-while/#findComment-1100160 Share on other sites More sharing options...
Pikachu2000 Posted August 17, 2010 Share Posted August 17, 2010 You could store the query result in an array, then echo the elements later in the script. Quote Link to comment https://forums.phpfreaks.com/topic/210922-selecting-multiple-rows-without-using-while/#findComment-1100161 Share on other sites More sharing options...
yarub Posted August 17, 2010 Author Share Posted August 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210922-selecting-multiple-rows-without-using-while/#findComment-1100163 Share on other sites More sharing options...
Alex Posted August 17, 2010 Share Posted August 17, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/210922-selecting-multiple-rows-without-using-while/#findComment-1100183 Share on other sites More sharing options...
yarub Posted August 17, 2010 Author Share Posted August 17, 2010 You rock! Thank you. Learned something new. Quote Link to comment https://forums.phpfreaks.com/topic/210922-selecting-multiple-rows-without-using-while/#findComment-1100186 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.