dadamssg Posted August 12, 2009 Share Posted August 12, 2009 I thought i could get an array from a query. When i run the query and then use a while loop to echo stuff out it works and i get 3 rows, which is right. But then when i var_dump or print_r the array i only get one result, not right. some exampe code $ble = "SELECT topic_id FROM newreplies WHERE user = 'admin' and viewed = '0' "; $res = mysqli_query($cxn,$ble) or die (mysqli_error($cxn)); $list = mysqli_fetch_assoc($res); then how do i get all the ROWS of $list? i only seem to get the first row pulled up Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 12, 2009 Share Posted August 12, 2009 mysqli_fetch_assoc <-- read that Quote Link to comment Share on other sites More sharing options...
dadamssg Posted August 12, 2009 Author Share Posted August 12, 2009 ok, maybe mysqli_fetch_assoc() isn't the function i need to use. Which one is and how would i use it to get what i want? Quote Link to comment Share on other sites More sharing options...
HoTDaWg Posted August 12, 2009 Share Posted August 12, 2009 hmmm i would suggest showing your full code.. or at least what you tried. what you are trying to does not sound very hard though, from the sound of it at least. Quote Link to comment Share on other sites More sharing options...
bundyxc Posted August 12, 2009 Share Posted August 12, 2009 You're printing one row. Print the result-set. print_r($res); Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 12, 2009 Share Posted August 12, 2009 ok, maybe mysqli_fetch_assoc() isn't the function i need to use. Which one is and how would i use it to get what i want? Well, yes it is, but did you READ the page I linked to? mysqli_fetch_assoc() returns the next row in the result set. If you only call it once, you'll only get the first one. It returns false when there are no next row. So what you want to do is to call it as long as it doesn't return false? How do we do that? Well, as you no doubt will have read in the manual, there is a control structure called a "while loop" that keeps running as long as its condition evaluates to true. So what we do is: while ($row = mysqli_fetch_assoc($res)) { // here we will keep selecting the next row and assign it to $row as long as a such next row exists // do stuff with each individual $row here } You know what? That looks surprisingly similar to what they proposed in one of the examples on the manual page for mysqli_fetch_assoc(). Reading the manual really answers a lot of questions. Who would have known that the manual tells you how to use it Quote Link to comment Share on other sites More sharing options...
dadamssg Posted August 12, 2009 Author Share Posted August 12, 2009 hey thanks for the responses. I'm going to be using the array i'm trying to get in a query. So if i use it with the while loop i'll make a BUNCH of queries whereas if i have one array that just holds topic_id numbers i can implode it and add commas and use one query Quote Link to comment Share on other sites More sharing options...
dadamssg Posted August 12, 2009 Author Share Posted August 12, 2009 now if you could help me construct an array using the while loop that would be MUCH appreciated Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 12, 2009 Share Posted August 12, 2009 hey thanks for the responses. I'm going to be using the array i'm trying to get in a query. So if i use it with the while loop i'll make a BUNCH of queries whereas if i have one array that just holds topic_id numbers i can implode it and add commas and use one query You'll still only execute the query once even if you call the fetch function multiple times. You are passing a resource to the fetch function that already contains information about the things that the query returned. now if you could help me construct an array using the while loop that would be MUCH appreciated Just create an empty array before the loop and add each $row to the array. Quote Link to comment Share on other sites More sharing options...
dadamssg Posted August 12, 2009 Author Share Posted August 12, 2009 how would i create an empty array? $array[0]; while ($list = mysqli_fetch_assoc($result)) { $array = $list['topic_id']; } Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 12, 2009 Share Posted August 12, 2009 You really should read the manual. <?php $ble = "SELECT topic_id FROM newreplies WHERE user = 'admin' and viewed = '0' "; $res = mysqli_query($cxn,$ble) or die (mysqli_error($cxn)); // <-- read the link in my signature regarding this $list = array(); while ($row = mysqli_fetch_assoc($res)) { $list[] = $row; } Quote Link to comment Share on other sites More sharing options...
dadamssg Posted August 12, 2009 Author Share Posted August 12, 2009 THANK YOU! Quote Link to comment Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 On this forums, it's sometimes just more effective to give a man a fish, than to teach him how. Quote Link to comment 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.