Solved

mysqli foreach loop issue...

Posted on 2013-05-30
11
607 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 150 total points
ID: 39209376
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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 39209385
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
ID: 39209413
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 110

Expert Comment

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

Expert Comment

by:Dave Baldwin
ID: 39209429
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
 

Author Comment

by:aej1973
ID: 39209801
How do I have the statement return arrays instead of individual values?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39209814
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
ID: 39209829
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 83

Expert Comment

by:Dave Baldwin
ID: 39209854
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
ID: 39209860
Dave, what do  I do to make $stmt4 return an array?
0
 

Author Comment

by:aej1973
ID: 39209873
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 dynamically set the form action using jQuery.

628 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