Solved

if all rows found are equal to each other

Posted on 2013-11-20
4
261 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
4 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
Comment Utility
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
Comment Utility
$query = "SELECT count(distinct field_qty_item) FROM ...";

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

Expert Comment

by:Chris Stanyon
Comment Utility
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
Comment Utility
smooth
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
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 …
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…
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…

762 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