-1

I'm struggling to understand this.

I have the following piece of code:

$result=$db->query(" select `job_id`,`jobs`.`client` 
       from `stores` 
       left join `jobs` 
       on `jobs`.`client`=`stores`.`id` 
       where `stores`.`postcode`='BD5 8HP' 
         and `stores`.`company`='MATALAN' 
         and `jobs`.`description`='MONTHLY external - Clean Both sides of External Glazing, Frames, Ledges & Entrance Doors (up to 1.8m Height) - Including Black out windows - Removing cobwebs and dust from all areas' ")->fetch();
echo "info:";
print_r($result);
die();

The query returns absolutely nothing, aka $result is an empty array.

If I run exactly the same query through PhpMyAdmin or from the MySQL binary client (on the server) I have reults.

The PHP code was a prepare -> execute construction, but since I was getting an empty result I tried to replace it (for test purpose only) with a direct query.

I had a look at a very similar topic on SO here which suggested that the problem was related to the binding, now I have no binding at all in above query, yet the result is an empty string.

Running other queries like, "select * from table" do return results, so I have NO clue on what can be wrong here.

Any idea/advice please?

As a footnote I'm running the query against a remote server. Can it be something with the charset?

EDIT:

While I received suggestion to change the value of DESCRIPTION, I cannot do that. That value is read out from a PDF and one location can have similar values, so using a LIKE and a % won't really work for me. I need to get the exact value matched.

EDIT 2 Ok the problem seems to be a bit more complex and the root cause of it might be somewhere deeper. If I manually write the string into the PHP code I do get the correct result, however when I pass it through as a variable it doesn't seem to work any more. Here is a bigger chunk of my script. The script is checking email with IMAP if an email matches some rules and it has attachment it opens the attachment which is a PDF, converts it to text, extract some images and updates a database accordingly.

$invoicedata=$a['attachment'];
                        $descriptorspec = array(
                           0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
                           1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
                           2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
                        );

                        $cwd = '/tmp';
                        $process = proc_open('pdftotext - -', $descriptorspec, $pipes, $cwd);

                        if (is_resource($process)) {

                            fwrite($pipes[0], $invoicedata);
                            fclose($pipes[0]);
                            $all_text=stream_get_contents($pipes[1]);
                            fclose($pipes[1]);

                            proc_close($process);
                            preg_match("/ARL SUPPORT SERVICES (.*)/",$all_text,$extracted);
                            $company=$extracted[1];
                            preg_match("/[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}/",$all_text,$extracted);
                            $postcode=$extracted[0];
                            preg_match("/Date\n*(.*-)/",$all_text,$extracted);
                            $date=trim(str_replace("/","-",str_replace("-","",$extracted[1])));

                            $date=date("Y-m-d H:i:s",strtotime($date));

                            preg_match("/SPECIFICATION:((.|\n)*)EQUIPMENT/",$all_text,$extracted);
                            $job_desc=trim($extracted[1]));

                            preg_match("/Resource\n*(.*)/",$all_text,$extracted);
                            $who_signed=trim(str_replace("-","",$extracted[1]));


                            $db = new PDO('mysql:host=XXX.XXX.XXX.XXX;dbname=ZZZZZZ;charset=utf8', 'USER','PASSWORD');
                            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                            $result=$db->query("select `job_id`,`jobs`.`client` from `stores` 
                            left join `jobs` on `jobs`.`client`=`stores`.`id`
                            where `stores`.`postcode`='$postcode' and `stores`.`company`='$company'
                            and `jobs`.`description`='$job_desc'")->fetch();
                        echo "info: ";
                        print_r($result);
                        die();

In this case the $result is empty!

If I do:

echo "select `job_id`,`jobs`.`client` from `stores` 
      left join `jobs` on `jobs`.`client`=`stores`.`id`
      where `stores`.`postcode`='$postcode' and `stores`.`company`='$company' and `jobs`.`description`='$job_desc'"
die();

It will just print:

select `job_id`,`jobs`.`client` from `stores` left join `jobs` on `jobs`.`client`=`stores`.`id` where `stores`.`postcode`='BD5 8HP' and `stores`.`company`='MATALAN' and `jobs`.`description`='MONTHLY external - Clean Both sides of External Glazing, Frames, Ledges & Entrance Doors (up to 1.8m Height) - Including Black out windows - Removing cobwebs and dust from all areas'

If I do copy back this into my PHP code then the code executes correctly, so the problem might be somewhere with converting the characters. I thought that it might be due to endash/emdash but checking the $job_desc with ord() returned char 45 for the - which is correct. also the & seem to be correct.

I'm running out of ideas, is there something so obvious which just doesn't jump out to me?

Community
  • 1
  • 1
Emil Borconi
  • 3,326
  • 2
  • 24
  • 40
  • empty array or empty string? i would say its a boolean named false ;) – Roland Starke Jun 21 '16 at 16:08
  • Have you tried error checking? For PDO error handling use `$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION):`. That will make it throw an exception so it's easier to see the issue. – Aroic Jun 21 '16 at 16:11
  • try another `description` :-) in your `WHERE` condition. or just `jobs.description LIKE 'MONTHLY external%'`. I am sure that will work. the problem is your string passed to the query. – Alex Jun 21 '16 at 16:13
  • Try only " select `job_id`,`jobs`.`client` from `stores` left join `jobs` on `jobs`.`client`=`stores`.`id`" and see what happens... ;-) – mlattari Jun 21 '16 at 16:18
  • I already have: $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); and there is no exception. $result is an empty string, if I do `->fetchAll()` then I have an empty array, sorry for confusion. Skipping the `jobs`.`description` part does return loads of results (more then I need). The description is a long varchar, which I hate but nothing I can do about it, and I do need to filter against it. As said running the exact same query from the command line will return results..... – Emil Borconi Jun 21 '16 at 16:21
  • Do you have an unique id in this row? Can you select it using this id? If so select and compare – Your Common Sense Jun 21 '16 at 16:56
  • TRY $result=$db->query(" select `client` from `jobs` where `description`='MONTHLY external - Clean Both sides of External Glazing, Frames, Ledges & Entrance Doors (up to 1.8m Height) - Including Black out windows - Removing cobwebs and dust from all areas' ")->fetch(); print_r($result); To establish if this is the problem. – david strachan Jun 21 '16 at 18:47
  • I have updated my question with a deeper explanation and more code. – Emil Borconi Jun 21 '16 at 21:03
  • Try isolate your problem. First, do a query with just one comparator: `where postcode='$postcode'`. After that, do another query with another one: `where company='$company'`. Then finally, `where description='$job_desc'`. See where is your problem and let us know. – Felippe Duarte Jun 21 '16 at 21:08
  • I have tried that, if I omit the `description='$job_desc' ` it will work. – Emil Borconi Jun 21 '16 at 21:11
  • Ok, then you could try this: `where description LIKE ".substr($job_desc,0,10)."%`. Then you can increase 10 to 20, 30 and so until you find which caracter is the problem here. – Felippe Duarte Jun 21 '16 at 21:49
  • Good tip, thanks, found the problem, I had a ASCII char(10) which on the screen printed as a space but actually it was a LR – Emil Borconi Jun 22 '16 at 08:56

1 Answers1

0

The problem was related with a wrong ASCII Char being fed from the PDFTOTEXT output.

implementing a str_replace function did solve the issue.

Thanks for all the support/suggestions.

Emil Borconi
  • 3,326
  • 2
  • 24
  • 40