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
Solved

mysqli foreach loop issue...

Posted on 2013-05-30
11
599 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 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 109

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 109

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP strings vs array errors 13 32
Is there a simpler dropbox system? 10 34
what do I need to host my own web sites? 13 50
simple html dom php accessing table/cell  values 4 19
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.

856 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