title: mysql: exact match?
body: <p>I'm pulling some data from mysql and counting occurrences of matches (seemingly equivalent to <code>WHERE foo='bar'</code>). However, as I loop thru the data in PHP, my count is much lower than the count in mysql.</p>
  <p><code>MYSQL&gt; SELECT COUNT(foo) FROM database.table WHERE foo='bar';</code></p>
  <pre><code># PHP
  while ($response = mysql_fetch_assoc($surveydata)){
      if ($response==='bar') {
          $bar++;
      }
  }
  </code></pre>
  <p>The data might contain one or more <code>&amp;</code>, so I want to match only <code>bar</code> and not <code>bar &amp; foobar</code>. I suspect mysql is counting <code>bar</code> and <code>bar &amp; foobar</code> whereas php is only counting <code>bar</code> and not <code>bar &amp; foobar</code>. Php is returning <code>1210</code>, and mysql is returning <code>1783</code>, so it's exactly practical to manually count to see who is right…</p>
  <p>I googled around, but was surprised nothing relevant came up for &quot;mysql exact match&quot; or &quot;mysql exactly equal&quot; x,x</p>
  <h1>EDIT</h1>
  <p>here's the actual sql</p>
  <pre><code>SELECT COUNT(`race`) FROM `database`.`table` WHERE `completion status`='complete';
  </code></pre>
  <p>and the actual php</p>
  <pre><code>mysql_query(&quot;SELECT `race`,`etcetera` FROM `database`.`table` WHERE `completion status`='complete';&quot;);
  
  $demographics=array(
      &quot;race&quot;=&gt;array(
          &quot;White&quot;=&gt;array('consented'=&gt;0,'partial'=&gt;0,'completed'=&gt;0),
          &quot;Black&quot;=&gt;array('consented'=&gt;0,'partial'=&gt;0,'completed'=&gt;0),
          &quot;Hispanic&quot;=&gt;array('consented'=&gt;0,'partial'=&gt;0,'completed'=&gt;0),
          &quot;Asian&quot;=&gt;array('consented'=&gt;0,'partial'=&gt;0,'completed'=&gt;0),
          &quot;Pacific Islander&quot;=&gt;array('consented'=&gt;0,'partial'=&gt;0,'completed'=&gt;0),
          &quot;Multiracial&quot;=&gt;array('consented'=&gt;0,'partial'=&gt;0,'completed'=&gt;0),
          &quot;Other&quot;=&gt;array('consented'=&gt;0,'partial'=&gt;0,'completed'=&gt;0)
      )
      //more
  );
  
  while ($dbrecord = mysql_fetch_assoc($surveydata)) {
      foreach ( $dbrecord as $dbfield=&gt;$dbcellval ) {
          foreach ( $demographics as $demographic=&gt;&amp;$options ) {
              foreach ( $options as $option=&gt;&amp;$counter ) {
                  if ( $option===&quot;Multiracial&quot; &amp;&amp; strpos($dbcellval,'&amp;') &gt;0 &amp;&amp; strpos($dbcellval,'&amp;')!==false ) {
                      if ($dbrecord['consent']===&quot;1&quot;){
                          $demographics[&quot;race&quot;][&quot;Multiracial&quot;]['consented']++;
                          if ($dbrecord['completion status']===&quot;partial&quot;) {
                              $demographics[&quot;race&quot;][&quot;Multiracial&quot;]['partial']++;
                          } // if
                          else if ($dbrecord['completion status']===&quot;complete&quot;) {
                              $demographics[&quot;race&quot;][&quot;Multiracial&quot;]['completed']++;
                          } // else if
                      } // if
                  }
                  else if ($option===$dbcellval){
                      if ($dbrecord['consent']===&quot;1&quot;){
                          $counter['consented']++;
                          if ($dbrecord['completion status']===&quot;partial&quot;) {
                              $counter['partial']++;
                          } // if
                          else if ($dbrecord['completion status']===&quot;complete&quot;) {
                              $counter['completed']++;
                          } // else if
                      } // if
                  } // else if $option==$item
              } // foreach $options
          } // foreach $demographics
      } // foreach $dbrecord
  } // while
  </code></pre>
  <p>the data from <code>SELECT race FROM database.table</code> looks like:</p>
  <pre><code>White &amp; Black
  White
  White &amp; Asian
  White &amp; Asian &amp; Black
  Asian
  Asian &amp; Black
  // etc
  </code></pre>