2012-07-07 217 views
0

我想格式化另一個網站數據插入到我的數據庫。他想關閉他的網站,所以給我他的網站列表。但即時通訊必須從他的flatfile數據庫格式化他的數據,進入我的mysql數據庫。搜索字符串while循環

Im循環訪問他的文本文件,並獲取他的值。然後根據需要進行格式化,然後將其插入我的數據庫。

因爲我們的網站使用完全不同的存儲格式和領域,即時通訊有一些問題。

我的網站有一個設計師領域。他沒有。所以即時通訊試圖通過他的描述字段搜索我的設計師表中找到一個匹配。如果有匹配,我想讓設計器ID插入設計器ID字段。但我不能讓這個代碼工作。

有人能請建議修復嗎?或者如果有更好的方法來做到這一點?

$fp = fopen('listings.txt','r'); 
if (!$fp) {echo 'ERROR: Unable to open file.'; exit;} 

$loop = 0; 

while (!feof($fp)) { 
$loop++; 
$line = fgets($fp,1024); //use 2048 if very long lines 
$field[$loop] = explode (' ', $line); 


$get_designers = mysql_query("SELECT * FROM dress_designers"); 
$row_designers = mysql_fetch_array($get_designers); 
$totalRows_designers = mysql_num_rows($get_designers); 

do{ 
// Note our use of ===. Simply == would not work as expected 
// because the position of 'a' was the 0th (first) character. 

$mystring = strtolower($field[$loop][8]); 
$findme = strtolower($row_designers['designer_name']); 
$pos = strpos($mystring, $findme); 

// Note our use of ===. Simply == would not work as expected 
// because the position of 'a' was the 0th (first) character. 
if ($pos === false) { 
$designer = "Other"; 
} else { 
$designer = "Siopa Rince"; 
} 

} while ($row_designers = mysql_fetch_assoc($get_designers)); 

$fp++; 
} 

fclose($fp); 

我只把「Siopa Rince」作爲測試。但這並不奏效。如果我從文件中獲取文本,並將其粘貼到$ mystring中,並將siopa rince放入$ findme中......它就可以工作。

任何建議將不勝感激!

感謝, 丹尼

OK ......怎麼樣剛進入信息的是什麼?我嘗試了一些不同的方法,但結果被返回null ...

我插入的數據後,生病使用搜索加入所需的行獲得一個ID:

SELECT dress_test.dress_title, (

SELECT dress_designers.designer_id 
FROM dress_designers 
WHERE MATCH (
dress_test.dress_desc 
) 
AGAINST (
'dress_designers.designer_name' 
IN boolean MODE 
) 
) AS real_designer_id 
FROM dress_test 

另一個版本:

SELECT dress_test.dress_title, dress_designers.designer_name 
FROM dress_test 
JOIN dress_designers ON MATCH(dress_test.dress_title, dress_test.dress_desc) AGAINST 
('dress_designers.designer_name' in boolean mode) 

其他建議?

+0

歡迎來到Stack Overflow!請不要將'mysql_ *'函數用於新代碼。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/KJveJ)。請參閱[**紅框**](http://goo.gl/GPmFd)?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定,[本文](http://goo.gl/3gqF9)將有助於選擇。如果你關心學習,[這裏是很好的PDO教程](http://goo.gl/vFWnC)。 – 2012-07-07 20:00:27

回答

0

你的第一項任務,以$row_designers使用mysql_fetch_array,而你的第二個使用mysql_fetch_assoc

而是做{...}同時,爲什麼不同時(){...}的

刪除此行$row_designers = mysql_fetch_array($get_designers);

,把你的循環變成...

while ($row_designers = mysql_fetch_assoc($get_designers)) { 
    // string search here 
} 

其他的一切看起來科幻ne - 如果遇到麻煩,請使用echo打印字符串或print_r來檢查打印數組的值。