ผมจะค้นหาแบบข้อความ เช่น ค้นหา คำว่า hello world
แต่ในฐานข้อมูลผมมี hello world ทำให้ค้นหาคำว่า hello world ไม่เจอ
มีวิธีไหนบ้าง ครับ ทำให้ ค้นหา เจอ ขอบคุณครับ
โดยปกติแล้ว เราจะใช้วิธีแยกข้อมูลค้นหาออกเป็นคำๆ ครับ เช่นจากโจทย์ เราจะคนหาคำว่า hello หรือ word ครับ
เวลาค้นหาก็จะได้ประมาณ
WHERE detail LIKE '%hello%' OR detail LIKE '%word%'
เวลาค้นหาก็จะได้ประมาณ
WHERE detail LIKE '%hello%' OR detail LIKE '%word%'
ลองดูตามนี้คับ (มือใหม่คับผม ^ ^)
$string="Hello World";
$words==preg_split("/[\s]+/",$string);#แยกออกทีละคำ โดยแบ่งตามช่องว่าง (" ")
$sql="SELECT * FROM tb_product WHERE pd_id <> '' AND ( ";
foreach($words as $word){
if($i<count($words)){#ตรวจสอบว่าถึงคำสุดท้ายหรือยัง
$sql.=" pd_detail LIKE '%$word%' OR ";
}else{#ถ้าถึงคำสุดท้ายแล้ว ให้เอาคำสั่ง OR ออก
$sql.=" pd_detail LIKE '%$word%'";
}
$i++;
}
$sql.=")";
$string="Hello World";
$words==preg_split("/[\s]+/",$string);#แยกออกทีละคำ โดยแบ่งตามช่องว่าง (" ")
$sql="SELECT * FROM tb_product WHERE pd_id <> '' AND ( ";
foreach($words as $word){
if($i<count($words)){#ตรวจสอบว่าถึงคำสุดท้ายหรือยัง
$sql.=" pd_detail LIKE '%$word%' OR ";
}else{#ถ้าถึงคำสุดท้ายแล้ว ให้เอาคำสั่ง OR ออก
$sql.=" pd_detail LIKE '%$word%'";
}
$i++;
}
$sql.=")";
LINK ======> LIKE
Quotations by Comment #3
LINK ======> LIKE
โทดทีคับ ^ ^"
ลองดูตามนี้คับ (มือใหม่คับผม ^ ^)
$string="Hello World";
$words==preg_split("/[\s]+/",$string);#แยกออกทีละคำ โดยแบ่งตามช่องว่าง (" ")
$sql="SELECT * FROM tb_product WHERE pd_id <> '' AND ( ";
foreach($words as $word){
if($i<count($words)){#ตรวจสอบว่าถึงคำสุดท้ายหรือยัง
$sql.=" pd_detail LINE '%$word%' OR ";
}else{#ถ้าถึงคำสุดท้ายแล้ว ให้เอาคำสั่ง OR ออก
$sql.=" pd_detail LINE '%$word%'";
}
$i++;
}
$sql.=")";
ขอบคุณครับ
ใจเย็นๆครับท่าน t007 พิมพ์ผิดอีกหรือเปล่านั่น
LINE or LIKE
แล้วก็ตรง if($i<count($words)){ ให้เพิ่ม -1 เป็นแบบนี้ครับ
if($i<count($words)-1){