Solved

mysqli foreach loop issue...

Posted on 2013-05-30
11
588 Views
Last Modified: 2013-05-30
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

0
Comment
Question by:aej1973
  • 5
  • 4
  • 2
11 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 150 total points
Comment Utility
It doesn't return an array so you can't use 'foreach' to go thru it.  It returns only the variables that you mentioned in $stmt4->bind_result($name);  See Example #1 here: http://us2.php.net/manual/en/mysqli-stmt.fetch.php
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
Comment Utility
Look for "MySQLi Complete Code Example Using Prepared Statements" near the end of this article.  It shows some sample code that should be applicable to your question.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Comment

by:aej1973
Comment Utility
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
What do you mean "make them go through a foreach loop?"  Did you check the article yet?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:aej1973
Comment Utility
How do I have the statement return arrays instead of individual values?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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.
0
 

Author Comment

by:aej1973
Comment Utility
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

0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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

0
 

Author Comment

by:aej1973
Comment Utility
Dave, what do  I do to make $stmt4 return an array?
0
 

Author Comment

by:aej1973
Comment Utility
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
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now