[英]SQL - Query fails *unless* string is broken up into number and letters and then concatenated
我有以下SQL查詢,該查詢通過DocNumber
返回發票:
"SELECT * FROM Invoice WHERE DocNumber = '" . $id . "'"
在這里, $id
包含一個類似於以下示例的字符串:
M10045
M10046
M10046
除非我將$id
分為'M'
和'10045'
並在查詢中將它們連接起來, 否則此查詢將失敗:
preg_match('/\d+/', $id, $id_number, PREG_OFFSET_CAPTURE);
preg_match('/[A-Z]+/', $id, $id_letter, PREG_OFFSET_CAPTURE);
"SELECT * FROM Invoice WHERE DocNumber = '" . $id_letter[0][0] . $id_number[0][0] . "'"
什么會引起這種問題?
注意,由於未找到匹配項,在查詢中使用$id
返回空數組。 拆分$id
的字母和數字部分將返回發票對象。
請試試
$id = trim($id);
要么
$id = preg_replace('/[^\w]/', '', $id);
$query = "SELECT * FROM Invoice WHERE DocNumber = '$id'";
刪除不是AlphaNum破折號或下划線的所有字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.