补充一下:这是检索网页并将内容分割为单个单词并写入索引表的代码。系统有5个表:定义搜索页面范围的表,停止词表,网页信息表(包含页面title和description),关键词表,索引表。foreach (str_word_count($file, 1) as $index => $word) { // words should be stored as lowercase for comparisons $word = strtolower($word); // skip word if it appears in the stop words list if (isset($stop_words[$word])) continue;
// determine if the word already exists in the database $query = sprintf('SELECT TERM_ID FROM %sSEARCH_TERM WHERE ' . 'TERM_VALUE = "%s"', DB_TBL_PREFIX, mysql_real_escape_string($word, $GLOBALS['DB'])); $result2 = mysql_query($query, $GLOBALS['DB']); if (mysql_num_rows($result2)) { // word exists so retrieve its id list($word_id) = mysql_fetch_row($result2); } else { // add word to the database $query = sprintf('INSERT INTO %sSEARCH_TERM (TERM_VALUE) ' . 'VALUE ("%s")', DB_TBL_PREFIX, mysql_real_escape_string($word, $GLOBALS['DB'])); mysql_query($query, $GLOBALS['DB']); // determine the word's id $word_id = mysql_insert_id($GLOBALS['DB']); } mysql_free_result($result2); // add the index record $query = sprintf('INSERT INTO %sSEARCH_INDEX (DOCUMENT_ID, ' . 'TERM_ID, OFFSET) VALUE (%d, %d, %d)', DB_TBL_PREFIX, $doc_id, $word_id, $index); mysql_query($query, $GLOBALS['DB']); }
另外你可能需要去看看sprintf的用法就明白了。
{
// words should be stored as lowercase for comparisons
$word = strtolower($word); // skip word if it appears in the stop words list
if (isset($stop_words[$word])) continue;
// determine if the word already exists in the database
$query = sprintf('SELECT TERM_ID FROM %sSEARCH_TERM WHERE ' .
'TERM_VALUE = "%s"',
DB_TBL_PREFIX,
mysql_real_escape_string($word, $GLOBALS['DB']));
$result2 = mysql_query($query, $GLOBALS['DB']);
if (mysql_num_rows($result2))
{
// word exists so retrieve its id
list($word_id) = mysql_fetch_row($result2);
}
else
{
// add word to the database
$query = sprintf('INSERT INTO %sSEARCH_TERM (TERM_VALUE) ' .
'VALUE ("%s")',
DB_TBL_PREFIX,
mysql_real_escape_string($word, $GLOBALS['DB']));
mysql_query($query, $GLOBALS['DB']); // determine the word's id
$word_id = mysql_insert_id($GLOBALS['DB']);
}
mysql_free_result($result2); // add the index record
$query = sprintf('INSERT INTO %sSEARCH_INDEX (DOCUMENT_ID, ' .
'TERM_ID, OFFSET) VALUE (%d, %d, %d)',
DB_TBL_PREFIX,
$doc_id,
$word_id,
$index);
mysql_query($query, $GLOBALS['DB']);
}
WROX_SEARCH_INDEX I%d ,$index
只是把$index组合表别名。