Solved

if all rows found are equal to each other

Posted on 2013-11-20
4
266 Views
Last Modified: 2013-11-21
How can we use if statement inside the foreach loop below to check whether all the row values (no matter what their count is) returned by the mysql query are equal to each other?

$query = "SELECT field_qty_item FROM ...";
foreach ($query as $row) {

   if all $row values returned are equal to each other...
   // do smthng
   
}
else {
// do nothing... or do another thing...
}
0
Comment
Question by:sbayrak
[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
4 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 39664471
There are many ways ... but this should work.
$FirstValSet = false;
$FoundMismatch = false;

$query = "SELECT field_qty_item FROM ...";
foreach ($query as $row) {
   if (!$FirstValSet){
    $val = $row[0];
    $FirstValSet = true;
  }else{
    if ($row[0] != $val){
      $FoundMismatch=true;
      break;
    }
  }
}
   if all $row values returned are equal to each other...
if (!$FoundMismatch){
   // do smthng
   
}
else {
// do nothing... or do another thing...
} 

Open in new window

0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 39664564
$query = "SELECT count(distinct field_qty_item) FROM ...";

if result == 1 then
  #they are all the same
else
 #they're not
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39665291
What exactly are you trying to achieve. Maybe the IF statement isn't the best way forward.

You can have a GROUP BY clause in your SQL Statement:

SELECT count(field_qty_item) as Amount, field_qty_item FROM yourTable GROUP BY field_qty_item

Open in new window

You would then only get unique values of field_qty_item, along with an Amount of how many times each value exists in the database. If you only get 1 record returned then clearly all the rows match each other.
0
 
LVL 1

Author Closing Comment

by:sbayrak
ID: 39667005
smooth
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 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