bigred Posted November 3, 2007 Share Posted November 3, 2007 My problem is when I use an array to store a record set, when I loop through, I can only display one field per row or everything in a row. I can't figure out how to reference specific fields. I've worked on this off and on for more than a month, and I haven't been able to do quite what I wanted. <?php $qu = "SELECT * from table1 where pnum > 100"; $result = mysql_query($qu) or die('Error getting result - mysql_error() = '.mysql_error()); $values = array(); while ($row = mysql_fetch_array ($result,MYSQL_ASSOC)) { $values[$row['pnum']] = $row; $columns = array_keys($row); } mysql_close($con); //this displays the data as I would like to see, but it's not a loop! echo $values[101][pnum]." - ".$values[101][name]."<br>"; echo $values[102][pnum]." - ".$values[102][name]."<br>"; echo $values[101][pnum]." - ".$values[101][pdesc]."<br>"; echo $values[102][pnum]." - ".$values[102][pdesc]."<br>"; //I want to display it dynamically, but can't figure out how to reference single fields //I have tried several different ways foreach ($values as $k => $v) { echo "k (this displays pnum) = ".$k; echo "but I can't reference ".$k[pnum]; echo "<br>"; } ?> perhaps I just need to do one query for one set of data, store in array A, then store another query in array B. They're the same records, but different data needs to be displayed in different areas. More details / other info: I recently gave up asp for php. in asp when working with record sets, I would: select * loop through all results and show A, B, C, and D go back to the first record loop through again but show A, B, F, and E This probably wasn't the right way to do it, but it's all I could figure out back then. I never figured out how to go back to the first record in php (but it would be a bonus to know). Instead, I am trying to store my results in an array because it sounds like it might be good practice (perhaps a wise idea), and it might enable me to do multiple queries and store them in multiple arrays, then display the data. I could really use some help. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/75861-solved-store-mysql-results-in-array-loop-but-only-show-certain-fields-almost-there/ Share on other sites More sharing options...
Barand Posted November 3, 2007 Share Posted November 3, 2007 <?php $qu = "SELECT * from table1 where pnum > 100"; $result = mysql_query($qu) or die('Error getting result - mysql_error() = '.mysql_error()); $values = array(); while ($row = mysql_fetch_assoc ($result)) { $values[] = $row; } foreach ($values as $row) { extract ($row); echo "$a $b $c $d <br>"; } foreach ($values as $row) { extract ($row); echo "$a $b $f $e <br>"; } ?> BONUS : to start again at row 0 mysql_data_seek($result, 0) Quote Link to comment https://forums.phpfreaks.com/topic/75861-solved-store-mysql-results-in-array-loop-but-only-show-certain-fields-almost-there/#findComment-383973 Share on other sites More sharing options...
bigred Posted November 3, 2007 Author Share Posted November 3, 2007 I appreciate you taking the time to look at this. That is exactly what I was looking for. You even got the bonus The example you gave will also help me clean up my code. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/75861-solved-store-mysql-results-in-array-loop-but-only-show-certain-fields-almost-there/#findComment-383983 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.