chop Posted August 11, 2011 Share Posted August 11, 2011 I thought there was a more direct way of doing this. I want to put the results of the query into the array "$itemDivs_array". Do I need to loop through like this? $q = "SELECT DISTINCT itemDivisionName from luItemTypes"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); echo "the number of rows is ".mysqli_num_rows($r)."<br />"; $kount = 0; while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) { echo $row['itemDivisionName'].", "; $itemDivs_array[$kount]=$row['itemDivisionName']; $kount += 1; } mysqli_close($dbc); thank you, chop Link to comment Share on other sites More sharing options...
HartleySan Posted August 11, 2011 Share Posted August 11, 2011 As far as I know, the while loop is the only way to loop through multiple rows returned from a MySQL query. Hopefully someone more knowledgeable can better answer your question. Also, is the counter variable necessary? Link to comment Share on other sites More sharing options...
Larry Posted August 12, 2011 Share Posted August 12, 2011 There's no better or easier way as far as I know. And the counter variable is not necessary. Link to comment Share on other sites More sharing options...
chop Posted August 12, 2011 Author Share Posted August 12, 2011 Thanks. I see what you mean about the counter index. Every once in a while I have flashbacks to my Basic or Pascal days. Although, wouldn't it be nice to be able to say something like: $shortDescription_array[]= mysqli_fetch_array ($r, MYSQLI_NUM) ?? chop Link to comment Share on other sites More sharing options...
Larry Posted August 12, 2011 Share Posted August 12, 2011 You can do exactly that. You just need to do it once for every record returned. So use a while loop, fetch the array into $row, then, within the loop, do $some_array[] = $row; Link to comment Share on other sites More sharing options...
chop Posted August 13, 2011 Author Share Posted August 13, 2011 Yes, I understand. I just removed the "$kount" subscript from my code (above) and it worked fine without it as it did with it. thanks for helping Link to comment Share on other sites More sharing options...
HartleySan Posted August 13, 2011 Share Posted August 13, 2011 You could also write your own function, within which the while loop is executed, and then you'd need only call the function once and pass it an argument or whatever. Link to comment Share on other sites More sharing options...
Recommended Posts