0

一般に、キーワードは MySQL フィールドで検索されます。

[tbl_hp] ID、タイトル

1, Harry Potter and the Philosopher's Stone
2, Harry Potter and the Chamber of Secrets
3, Harry Potter and the Prisoner of Azkaban
4, Harry Potter and the Goblet of Fire
5, Harry Potter and the Order of the Phoenix
6, Harry Potter and the Half-Blood Prince
7, Harry Potter and the Deathly Hallows

PHP

$keyword = "Philosopher";
$keyword = "Harry Potter";

SQL

SELECT `id` FROM `tbl_hp` WHERE `title` LIKE '%" . $keyword . "%';

結果は次のとおりです。

"Philosopher" => 1
"Harry Potter" => 1,2,3,4,5,6,7

しかし、これを行う方法は?

[tbl_hp_keywords] ID、キーワード

1, philosopher
2, chamber
3, azkaban
4, goblet
5, phoenix
6, prince
7, hallows
8, prisoner
9, Prisoner of Azkaban
10, Half-Blood Prince

PHP

$keyword = "Harry Potter and the Chamber of Secrets";
$keyword = "Harry Potter and the Prisoner of Azkaban";
$keyword = "Harry Potter and the Half-Blood Prince";

結果は次のとおりです。

"Harry Potter and the Chamber of Secrets" => 2
"Harry Potter and the Prisoner of Azkaban" => 3,8,9 => or even better result is 9
"Harry Potter and the Half-Blood Prince" => 6,10 => or even better resurt is 10

これを行う方法に関するヘルプやアイデア...事前に感謝します:)

4

1 に答える 1

0
<?php
$keywords = explode(' ', $keyword);
foreach($keywords AS $key){
    $sql = "SELECT id FROM tbl_hp_keywords WHERE keyword = '$key'";
    $query = mysql_query($sql);
    while($result = mysql_fetch_array($query)){
        $ids[] =$result['id'];
    }
}
$answer[$keyword] = implode(',', $ids);

またはこれを試してください

$keyword = 'Harry Potter and the Chamber of Secrets';
$keywords = explode(' ', $keyword);
$modifiedKeyword = implode("', '", $keywords);
$sql = "SELECT id FROM tbl_hp_keywords WHERE keyword IN ('$modifiedKeyword')";
try {
    $hostname = 'servername';
    $dbname = 'dbname';
    $username = 'username';
    $pw = "password";
    $pdo = new PDO("mssql:host=$hostname;dbname=$dbname", $username, $pw);
} catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
}
$query = $pdo->prepare($sql);
$query->execute();

for ($i = 0; $row = $query->fetch(); $i++) {
    $ids[] = $row['id'];
}

unset($pdo);
unset($query);
于 2012-09-04T14:17:38.683 に答える