strcmp() not returning zero for match

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

strcmp() not returning zero for match

Post by Bill H »

The following code works fine when the user leaves the entry blank (to view all records in the database), but not when he makes an entry (in $_POST['Last'] entry) to omit the first reports. By use of print() and print_r() I have verified that everything is functioning as expected and that at one point there is a match.

$Row['Numbr'] is a varchar and never contains escaped characters. The entry, for instance, of 2457 in the $_POST should match to a database field content of 2457, but that entry does occur and does not reset the $Flag variable to zero, and I simply am missing the reason why it does not.

Code: Select all

<?php
if (isset($_POST['Last']))               // user clicked okay button: put all reports into
{                                        // a session array to be used for printing sequence
     $_SESSION['Rcnt'] = 0;                            // the count of number to be done
     $_SESSION['Doing'] = 0;                           // the one currently doing
     $Flag = strlen($_POST['Last']);                   // greater than zero if entry made

     $Query = "SELECT id FROM Stores ORDER BY Score,Num";
     $Oresult = mysql_query($Query, $Link);            // get stores by owner, number
     while ($Orow = mysql_fetch_array($Oresult))       // owner based on sorting variable in the stores
     {
          $Query = "SELECT id,Numbr FROM Reports WHERE Sid=" . $Orow['id'];
          $Result = mysql_query($Query, $Link);
          if ($Row = mysql_fetch_array($Result))       // there will only be one report per store
          {    if (!$Flag)
               {    $_SESSION['Rcnt']++;                         // bump the array counter
                    $_SESSION['Run'][] = $Row['id'];             // add report to the array
               }
               elseif (!strcmp($_POST['Last'],$Row['Numbr'])) $Flag = 0;
          }
     }
}
?>
Anyone see what I'm missing?
:?:
RadixDev
Forum Commoner
Posts: 66
Joined: Sun Mar 14, 2004 11:27 am
Location: U.K.

Post by RadixDev »

Just one thing, why do you need to use strcmp, there's no binary coming into this number comparison is there? Everything else seems to be OK. Have you tried printing the value for $Result and see whether it actually matches with the value you inputed in the form?
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: strcmp() not returning zero for match

Post by TheBentinel.com »

Bill H wrote: elseif (!strcmp($_POST['Last'],$Row['Numbr'])) $Flag = 0;
Could you have some leading/trailing spaces that are throwing it off? "2457" wouldn't be equal to "2457 "

It might also be useful to see what the strcmp function is returning:

Code: Select all

else
{
  print ("<hr>_" . $_POST['Last'] . "_<hr>");  
  print ("<hr>_" . $Row['Numbr'] . "_<hr>");  
  print ("<hr>_" . strcmp($_POST['Last'],$Row['Numbr']) . "_<hr>");
  if (!strcmp($_POST['Last'],$Row['Numbr'])) $Flag = 0;
}
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

RadixDev: I should have mentioned why the store "number" is stored in the database as a string. It is not always a pure number: is sometimes something like 2354-1, for instance, and thus the need for the strcmp() function.

TheBentinel.com: thanks

Code: Select all

<?php
elseif (!strcmp($_POST['Last'],trim($Row['Numbr']))) $Flag = 0;
?>
fixed it. Now I just need to figure out how the padding got into the database, because I don't recall putting it there, and it doesn't show when using phpmyadmin. Very interesting.
:?
Edit, a few hours later: okay, I did put the padding in the database. Getting old. Memory may not be the first thing that goes, but...
:oops:
Post Reply