Jump to content

[SOLVED] array confusion


dadamssg

Recommended Posts

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

Link to comment
Share on other sites

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 :sarcastic:

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

  • 4 weeks later...
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.