11 Replies Latest reply on Nov 10, 2012 7:12 AM by brianread

    Using PHP API Matching a whole character field with a complete string

    brianread

      I have this:

       

      $findCommand->addFindCriterion("First Name","==Ann");

      $findCommand->addFindCriterion("Surname","Smith");

       

      But am getting back a number of records, including ones where the "First Name" is "Anne".

       

      http://www.chorleytradmusicsessions.org.uk/images/FilemakerSearchResult.png

      I understood that the "==" mean't that the whole field should be compared.

       

      This is Filemaker V12.

       

      Any ideas?

        • 1. Re: Using PHP API Matching a whole character field with a complete string
          mbraendle

          Not sure what goes wrong with your code. Maybe you should show us a larger code fragment so that we can find out.

           

          Here on FMS11 it is working exactly as you described.

          • 2. Re: Using PHP API Matching a whole character field with a complete string
            brianread

            Ok, the problem is that the find returns a number of records (as per screenshot), one of which has "Ann" in the "firstname" field, and the others have "Anne".

             

            I expected that the find command "==Ann" would match the full string to the full field, and thus only return the "Ann" record.  If my understanding is wrong, then I am happy to be told, but also I need to know how to achieve my aim of a complete match both sides.

             

            Here is more details of my code:

             

            $fm = new FileMaker(FM_FILE, FM_HOST, FM_USER, FM_PASS);

             

            $findCommand =& $fm->newFindCommand('Contacts');

            $findCommand->addFindCriterion("First Name","==Ann");

            $findCommand->addFindCriterion("Surname","Smith");

             

            // Execute find command

            $result = $findCommand->execute();

             

            if (FileMaker::isError($result)) {

                echo "Error: " . $result->getMessage() . "\n";

                exit;

            }

             

            // Get array of found records

            $records = $result->getRecords();

             

            // Print out found records

             

            // Setup row count variable to alternate row background color

            $row = 0;

             

            foreach ($records as $record) {

                // if $row is odd, set class of <tr> to alt-row-color

                if ($row % 2 == 0) {

                    echo "<tr class=\"alt-row-color\">";

                } else {

                    echo "<tr>";

                }

                    echo "<td class=\"align-left\"><a href=\"viewRecord.php?recid=" . $record->getRecordId() . "\">" . $record->getField('Contact Id') . "</td>";

                    echo "<td>" . $row . "</td>";

                    echo "<td>" . $record->getField('First Name') . "</td>";

                    echo "<td>" . $record->getField('Surname') . "</td>";

                    echo "<td>" . $record->getField('Date of Birth') . "</td>";

                    echo "<td>" . $record->getField('Meditators::Meditator ID') . "</td>";

                     echo "<td>" . $record->getField('TM Instructions::Date of Instruction') . "</td>";

                     echo "<td>" . $record->getField('Email Addresses::Email Address') . "</td>";

             

                    echo "</tr>";

                    $row++;

            }

            ?>

            <tr><td colspan="3"><a href="editRecord.php">Create New Record</a></tr>

            </table>

            </body>

            </html>

            • 3. Re: Using PHP API Matching a whole character field with a complete string
              mbraendle

              I understood your problem, but you probably didn't understand me. What I wanted to say was that the behaviour is correct if one adds a == in front of the query value --> exact field match.

               

              You can test as follows:

               

              http://www.infochembio.ethz.ch/test/test2.php?title=Organic%20Chemistry&author=Carey

               

              http://www.infochembio.ethz.ch/test/test2.php?title===Organic%20Chemistry&author=Carey

               

               

              Here the code:

               

              <?php

              error_reporting(E_ERROR);

              ini_set('display_errors', 'on');

              include_once('./libs/filemaker/FileMaker.php');

              include_once('fm_config.php');

               

               

              if (array_key_exists('title', $_REQUEST)) {

                $title = $_REQUEST['title'];

              } else {

                $title = 'Physical Chemistry';

              }

               

               

              if (array_key_exists('author', $_REQUEST)) {

                $author = $_REQUEST['author'];

              }

               

              $fm = new FileMaker('Katalog',C_FM_HOST,C_FM_USER,C_FM_PASSWORD);

              $find = $fm->newFindCommand('WWW_Results');

              $find->addFindCriterion('Titel',$title);

              $find->addFindCriterion('Urheber',$author);

              $find->setRange(0,1000);

              $result = $find->execute();

               

               

              if (FileMaker::isError($result)) {

                $error_code = $result->getCode();

                if ($error_code == 401) {

                  header('Content-type: text/xml');

                  echo '<?xml version="1.0" encoding="UTF-8"?><books count="0"/>';

                } else {

                  echo 'A FileMaker Error occured: Code'.$error_code.'<br/>';

                  echo $result->getErrorString().'<br/>';

                  echo $result->getMessage();

                }

                die();

              }

               

              $records = $result->getRecords();

              $count = $result->getFoundSetCount();

              header('Content-type: text/xml');

              echo '<?xml version="1.0" encoding="UTF-8"?>';

              echo '<books count="' . $count . '">';

              foreach ($records as $record) {

               

                echo '<book title="' . $record->getField('Titel') . '" author="' . $record->getField('Urheber') . '" />';

              }

              echo '</books>';

              ?>

               

               

              I see a small difference in your declaration of the $findCommand object: What happens if you remove the ampersand?

              • 4. Re: Using PHP API Matching a whole character field with a complete string
                brianread

                Ok, then I can see that it is comparing the "start" of the field, then how can I tell it that the compare string is mean't to be the total field?

                 

                The ampersand just means the record is passed back as a reference I think, I got that from the filemaker supplied examples.  I took it out and it made no difference.

                • 5. Re: Using PHP API Matching a whole character field with a complete string
                  mbraendle

                  Of course and obviously, if the total field is being matched using the == operator, this is done from the field start ...  (Please stop this nit-picking. You saw my code and can conclude from it that no field content is being changed during output).

                   

                  There might be many reasons why your == search does not yield the expected result:

                   

                  - Index corruption (which might be relieved by rebuilding the index)

                  - database corruption

                  - Bug in PHP API that comes with FMS 12

                  - Bug in FMS 12

                   

                  Please repeat the search directly in the FM database and check whether you encounter the problem there as well. Then it might be index corruption.

                   

                  Please also state the version of FMS and the used PHP.

                  • 6. Re: Using PHP API Matching a whole character field with a complete string
                    brianread

                    >There might be many reasons why your == search does not yield the expected result

                     

                    what I really want to know is  the "==" operator mean't to only compare "up to" the length of the Compare string OR is it mean't to compare the whole field contents?

                     

                    Your example seemed to indicate the former, which agrees with my experience.  Which seems out of line with what FMs documentation implies. However, I'd like to know if you know how to make it compare the whole field and fail if there are characters beyond the compare string.

                     

                    APOLOGIES - I now realuse this is not true, when I make you send string ===Cary I get nothing back - this is the result I want.

                     

                    I'll test it on the Client direct.

                     

                    I am on FMS 12 as well as far as I can tell (it is remotely hosted by a third party).  The php file is one I found on the FM site, I do not have access to the Server machine to get the Filemaker.php that is on there.  It does not seem to have any version information, except a copyright 2005. It does compain about deprecated php when I run it (until i supressed those messages in the php.ini).  Perhaps I have an earlier one.  Do you have a link to get me the latest?  php is V5.3 in this case running under Ubuntu-server.

                     

                    I do appreciate your help - I am a little bit unhappy that the FM company tself does not seem to consider supporting its customers.

                    • 7. Re: Using PHP API Matching a whole character field with a complete string
                      brianread

                      Ok, on the FM client, the search works as expected.  so it looks as though we have an FM PHP interface issue.

                       

                      I need to make sure I am on the latest PHP I guess. 

                      • 8. Re: Using PHP API Matching a whole character field with a complete string
                        mbraendle

                        Yes, the == operator compares the whole field content, as you had just found out yourself with

                         

                        http://www.infochembio.ethz.ch/test/test2.php?title=Organic%20Chemistry&author===Carey

                         

                        which yields 0 records, because in our DB the author field not only holds the family name, but always initials or the first name and sometimes the middle name as well.

                         

                        If I find time to install a test installation of FMS 12, I can countercheck whether it is a problem with this version.

                        • 9. Re: Using PHP API Matching a whole character field with a complete string
                          brianread

                          Ok, I have now managed to get hold of the latest filemaker.php and associated files, and the whole field search now works as advertised! 

                           

                          Many thanks for the help.

                          • 10. Re: Using PHP API Matching a whole character field with a complete string
                            TimDietrich

                            Brian --

                             

                            In looking at your code and example screenshot, I think the reason you are getting the unexpected results is this line:

                             

                            $findCommand->addFindCriterion("First Name","==Ann");

                             

                            I think your field name is "Firstname" - and as a result, the criteria for the name ois simply being ignored.

                             

                            I believe the line should be:

                             

                            $findCommand->addFindCriterion("Firstname","==Ann");

                             

                            Hope this helps!

                             

                            -- Tim

                            • 11. Re: Using PHP API Matching a whole character field with a complete string
                              brianread

                              Hi Tim

                               

                              thanks for your reply, but as you may see, I have solved the problem by making sure I had the latest version of the filemaker.php file.

                               

                              I'm afraid your assessment is wrong, the field name is "First Name", see:

                               

                              echo "<td>" . $record->getField('First Name') . "</td>"; 

                               

                              You are being confused by the column heading!

                               

                              I agree that spaces in a field name is strange, but it is legal!  I may say I DID NOT setup this DB!

                               

                              Cheers Brian