我很努力去理解這一點。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