2017-10-21 29 views
0

難道有人會告訴我我該如何做到這一點,或者如果這不可能,那麼以另一種方式展示我?如何使用數組值或類似的選擇數據,在PHP的MySQL?

我試圖得到這樣的描述:用戶輸入並切掉一些單詞,然後獲取第一個單詞並搜索數據庫中是否有另一個單詞,就像它在與該單詞相關的單詞的描述中一樣

這整個事情是有點像一本字典

$word = (isset($_POST['word']) ? $_POST['word'] : null); 
$description = (isset($_POST['description']) ? $_POST['description'] : 
null); 
echo "<br>" . "<br>";//this is why its showing 
$Slicer = array(' a ', 'A ', 'The ', ' the ', ' an ', ' this ', ' that 
', 
' these ', ' those ', ' my ', ' your ', ' his ', ' her ', ' its ', 
' it\'s ', ' our ', ' their ', ' few ', ' little ', ' much ', ' many ', 
' lot ', ' of ', ' most ', ' some ', ' any ', ' enough ', ' all ', ' 
both ', 
' either ', ' neither ', ' each ', ' every ', ' other ', ' another ', 
' Here ', ' I ', ' me ', ' mine ', ' myself ', ' i ', 
' you ', ' your\'s ', ' yourself ', ' he ', ' him ', ' his ', 
' himself ', ' she ', ' her\'s ', ' herself ', ' itself ', 
' we ', ' us ', ' our ', ' ours ', ' ourselves ', ' yourselves ', 
' they ', ' them ', ' theirs ', ' themselves ', ' no ', ' none ', 
' not ', ' any ', ' few ', ' few ', ' several ', ' great ', ' deal ', 
' lot ', ' lots ', ' large ', ' amount ', ' plenty ', ' more ', 
' fewer ', ' fewest ', ' less ', ' least ', ' what ', 'she\'s', 
'the ', ' to ', ' for ', ' something ', ' or ', ' used ', 
' represent ', ' in ', ' by ', ' are ', ' often ', ' called ', 'a ', 
'.'); 

$sliced = str_replace($Slicer,' ',$description); 
echo $sliced; 
echo "<br>"; 
$SWords = (explode(" ",$sliced)); 
echo "<br>"; 

$FirstWord = $SWords[1]; 
echo "<br>"; 
echo $FirstWord; 
echo "<br>"; 
$test = "test"; 

$sql = "SELECT * FROM WordDatabase WHERE description LIKE '" 
.$FirstWord."'"; 

我有更多的代碼和Im連接到我的數據庫和一切

回答

0

1是不是在PHP中的數值數組的第一個索引。在PHP(和幾乎所有其他的編程語言)的所有指標爲0。

啓動,以便第一個字將在

$FirstWord = $SWords[0]; 

你最有可能使用類似錯誤。像沒有通配符(%will in most cases (but not allways)的結果與=相同。所以WHERE description LIKE 'foo'可能會做WHERE description = 'foo'(只有可能更慢),這很可能是而不是你想要的。

也許你想是這樣的:

SELECT * FROM WordDatabase WHERE description LIKE 'foo%' 

SELECT * FROM WordDatabase WHERE description LIKE '%foo%' 

另外,你的代碼是SQL注入攻擊可能容易受到影響。請不要將用戶輸入直接連接到查詢。相反,使用mysqliPDO或利用這兩個中的一個框架,並用自己的方法來創建一份準備好的聲明是這樣的:

$mysqli = new mysqli($host, $user, $pass, $db); 
if (mysqli_connect_errno()) { 
    throw new Exception('Could not connect to database: ' . mysqli_connect_error()); 
} 
if ($sql = $mysqli->prepare("SELECT description FROM WordDatabase WHERE description LIKE ?")) { 
    $sql->bind_param("s", $FirstWord . '%'); 
    $sql->execute(); 
    $sql->bind_result($description); 
    $sql->fetch(); 
    $sql->close(); 
} else { 
    # .... 
} 
$mysqli->close(); 
+0

謝謝你的建議和解決方案! –

+0

「SELECT * FROM WordDatabase WHERE description LIKE'%$ FirstWord%''」 –

相關問題