我已經創建了一個使用PHP的搜索欄,我想檢索我在MySQL數據庫中的數據。如何使用PHP搜索欄檢索MySQL數據庫數據
我的代碼如下:
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(!$button)
echo "You searched for '<b>$search</b>' <hr size='1'</br>";
$con=mysqli_connect("localhost", "root", "root", "PM_DB");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="keywords LIKE '%$search_each%'";
else
$construct .="AND keywords LIKE '%$search_each%'";
}
$construct ="SELECT * FROM Leads WHERE $construct";
$run = mysqli_query($construct);
$foundnum = mysqli_num_rows($run);
if ($foundnum==0)
echo "There are no results for <b>'$search'</b>. Please check your spelling.";
else
{
echo "$foundnum results found !<p>";
while($runrows = mysqli_fetch_assoc($run))
{
$Company = $runrows['Clients'];
echo "<a href='$Company'><b>Company</b></a><br>";
}
}
?>
的每一次點擊搜索它只返回錯誤信息。我錯過了什麼?任何建議將不勝感激!謝謝 - Tijger。
提供了錯誤信息 – 2014-10-31 09:30:16
它給錯誤或打印沒有結果被發現?另外,'$ x'還沒有在循環前的任何地方初始化。 – 2014-10-31 09:30:19
它顯示沒有找到結果,可以,因爲$ x沒有在循環之外初始化,所以沒有返回任何東西? – Tijger 2014-10-31 09:39:31