我很努力去理解这一点。PDO在PHP中查询没有结果,但在phpmyadmin或mysql二进制文件中产生结果
我有下面的代码:
$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();
查询返回绝对没有,又名$结果是一个空数组。
如果我通过PhpMyAdmin或从MySQL二进制客户端(在服务器上)运行完全相同的查询,我有reults。
PHP代码是一个准备 - >执行构造,但由于我得到一个空的结果,我试图用直接查询替换它(仅用于测试目的)。
我看过SO here上的一个非常相似的主题,它表明问题与绑定有关,现在我在上面的查询中根本没有绑定,但结果是一个空字符串。
运行其他查询,如"select * from table"
确实会返回结果,所以我不知道这里可能会出现什么问题。
有什么想法/建议吗?
作为一个脚注,我正在对远程服务器运行查询。它可以是字符集的东西吗?
编辑:
虽然我收到建议修改描述的值,我不能这样做。该值是从PDF中读出的,而且一个位置可以具有相似的值,因此使用LIKE和%并不适用于我。我需要得到匹配的确切值。
编辑2 好吧,问题似乎有点复杂,它的根本原因可能更深层次。如果我手动将字符串写入PHP代码,我会得到正确的结果,但是当我将它作为变量传递时,它似乎不再起作用。这是我的脚本的一大块。如果电子邮件符合一些规则,脚本正在检查电子邮件,并且它具有附件,它会打开PDF附件,将其转换为文本,提取一些图像并相应地更新数据库。
$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();
在这种情况下,$result
是空的!
如果我做的:
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();
它只是打印:
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'
如果我不复制回这为我的PHP代码,然后将代码正确执行,所以这个问题可能是某处转换角色。我认为这可能是由于endash/emdash,但检查$job_desc
与ord()
返回字符45 - 这是正确的。也是&似乎是正确的。
我用尽了想法,是否有那么明显的东西,只是不跳出来给我?
空数组或空字符串?我会说它是一个名为false的布尔值;) –
你试过错误检查吗?对于PDO错误处理,使用'$ db-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION):'。这将使它抛出一个异常,以便更容易看到问题。 –
在你的WHERE条件下尝试另一个'description' :-)。或者只是'jobs.description LIKE'MONTHLY external%''。我相信这会起作用。问题是你的字符串传递给查询。 – Alex