Solved

mysqli foreach loop issue...

Posted on 2013-05-30
11
591 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 108

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
 
LVL 108

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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

920 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

13 Experts available now in Live!

Get 1:1 Help Now