Link to home
Start Free TrialLog in
Avatar of aej1973
aej1973

asked on

mysqli foreach loop issue...

Hi, I have a mysqli code as attached. When I run this script I get the following error:

Warning: Invalid argument supplied for foreach() in /var/www/voice/group_create.php on line 251

Can someone let me know how I can correct this issue?

$stmt4 = $mysqli->prepare("SELECT name from queue_table where user_id =? group by name");
$stmt4->bind_param('i',$_SESSION['user_id']);
$stmt4->execute();
$stmt4->bind_result($name);

.....
....
  while($stmt4->fetch()){
	foreach($name as $key){
	

.....
...
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aej1973
aej1973

ASKER

Dave, when I run the following query:

while($stmt4->fetch())
 {
     echo $name;
       
         }

it returns two values; 101,102. I am just running these two values through a foreach loop and I am getting an error. What do I do to make values 101 and 102 go through a foreach loop? Thanks.

A
What do you mean "make them go through a foreach loop?"  Did you check the article yet?
There is nothing for a 'foreach' loop to work on.  Your code with while($stmt4->fetch()) returns individual values, not arrays.  What is it you are expecting?  If you are comparing this to the results you would get from a mysql_fetch_array, don't.  It's not the same thing.  while($stmt4->fetch())  returns Individual values, not rows.  The values are declared in the $stmt4->bind_result($name); line.  $name is the only value you have declared there.
Avatar of aej1973

ASKER

How do I have the statement return arrays instead of individual values?
In your example there is no point.  Your Select statement is only asking for 'name' to be returned for each row so even if you could make it an array, it would still only have one value in it.

Why do you want it to be an array?  It is giving you the data you ask for the way it is.
Avatar of aej1973

ASKER

Hi Dave, I made a few changes to my statement. What I am trying to achieve to be able to output the all rows that match a particular value. My code is attached, not sure what I am doing wrong, thanks for the help.

$stmt4 = $mysqli->prepare("SELECT name from queue_table where user_id =? group by name");
$stmt4->bind_param('i',$_SESSION['user_id']);
$stmt4->execute();
$arr = array();
$stmt4->bind_result($name);

 while($stmt4->fetch()){
		  $arr[] =  $name;
	          foreach($arr as $n){

$stmt3=$mysqli->prepare("SELECT g.group_number,g.group_name,m.mailbox_no,q.strategy from groups g, mailbox m, queue_table q where g.group_number=m.group_extension and g.group_number=q.name and g.group_number=? group by g.group_number");
$stmt3->bind_param('i',$n);
$stmt3->execute();
$stmt3->bind_result($group_number,$group_name,$mail,$ringType);
while($stmt3->fetch()){   
      echo $group_name;	  
  	
    }};

Open in new window

You're not understanding that $name is a single value.  It is not and will not be an array.  For what you appear to be trying, you don't need the 'foreach', especially since it has nothing to work with.  This may work.
while($stmt4->fetch()){
$stmt3=$mysqli->prepare("SELECT g.group_number,g.group_name,m.mailbox_no,q.strategy from groups g, mailbox m, queue_table q where g.group_number=m.group_extension and g.group_number=q.name and g.group_number=? group by g.group_number");
$stmt3->bind_param('i',$name);
$stmt3->execute();
$stmt3->bind_result($group_number,$group_name,$mail,$ringType);
while($stmt3->fetch()){   
      echo $group_name;  	
    }
};

Open in new window

Avatar of aej1973

ASKER

Dave, what do  I do to make $stmt4 return an array?
Avatar of aej1973

ASKER

ok, I have resolved the issue. after $stmt4->bind_result($name); I had to have $stmt4->store_result();.  Once I had this statement in place, the following script worked:

while($stmt4->fetch()){
$stmt3=$mysqli->prepare("SELECT g.group_number,g.group_name,m.mailbox_no,q.strategy from groups g, mailbox m, queue_table q where g.group_number=m.group_extension and g.group_number=q.name and g.group_number=? group by g.group_number");
$stmt3->bind_param('i',$name);
$stmt3->execute();
$stmt3->bind_result($group_number,$group_name,$mail,$ringType);
while($stmt3->fetch()){  
      echo $group_name;        
    }
};

Thanks for you time and help.

A