本帖最后由 mylsok 于 2012-01-01 11:43:03 编辑

解决方案 »

  1.   

    你输出$query就知道是什么值了。
    另外你可能需要去看看sprintf的用法就明白了。
      

  2.   

    补充一下:这是检索网页并将内容分割为单个单词并写入索引表的代码。系统有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']);
        }
      

  3.   

    多谢,sprintf等sql语句我都明白,只是这里的 $index 感觉不对,为什么拿用户输入的关键词数组的索引值去比较索引表和关键词表中的id呢?
      

  4.   

    你还是没能理解sprintf在此的意图。。
    WROX_SEARCH_INDEX I%d ,$index
    只是把$index组合表别名。
      

  5.   

    谢谢,确实是。不过,如果省略这里的 $index,不组合表别名,每次查询时会出错么?或者说,一定要这样组合每次查询的表别名吗?
      

  6.   

    最好是加上别名。因为你前面的FROM WROX_SEARCH_DOCUMENT D 已经为表起别名了。 再加上foreach。。起别名不容易起冲突。况且多表连接一般都要起别名的。