2014-10-31 235 views
0

我已經創建了一個使用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。

+0

提供了錯誤信息 – 2014-10-31 09:30:16

+0

它給錯誤或打印沒有結果被發現?另外,'$ x'還沒有在循環前的任何地方初始化。 – 2014-10-31 09:30:19

+0

它顯示沒有找到結果,可以,因爲$ x沒有在循環之外初始化,所以沒有返回任何東西? – Tijger 2014-10-31 09:39:31

回答

0

試試這個代碼

$search_exploded = explode(" ", $search); 

$construct = "SELECT * FROM Leads WHERE keywords LIKE "; 
$construct .= "'%".implode("%' OR '%", $search_exploded)."%'"; 

$run = mysqli_query($construct); 

Live demo

+0

只返回「SELECT * FROM Leads WHERE關鍵字LIKE%company%'沒有'company'的結果,請檢查您的拼寫。」 – Tijger 2014-10-31 09:47:01

+0

查看更新的答案...我忘記了單引號.... 現在就使用上面的代碼 – Umair 2014-10-31 10:08:37

+0

它仍然只是返回「沒有結果的公司」,請檢查您的拼寫。 – Tijger 2014-10-31 10:43:51

0

正如我所要求的代碼從OP

我評論兩線,分別造成誤差爲他

他被執行權查詢,然後再次錯誤的查詢...這就是爲什麼沒有返回結果。

<?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); 

     $construct = "SELECT * FROM Leads WHERE keywords LIKE "; 
     $construct .= "'%" . implode("%' OR '%", $search_exploded) . "%'"; 

     $run = mysqli_query($construct) or die(mysql_error()); 

     // Why thisssssssssssssssssss??????????? OMG 
     //$construct = "SELECT * FROM Leads WHERE $construct"; 
     //$run = mysqli_query($construct); 

     $foundnum = mysqli_num_rows($run) or die(mysql_error()); 

     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>"; 
      } 
     } 
     ?> 
+0

仍然返回空白頁。 – Tijger 2014-10-31 11:31:56

+0

嗯我不知道,但也許你也許有一些錯誤。 add 'error_reporting(E_ALL)'在您的PHP代碼的頂部,看看是否有任何錯誤 – Umair 2014-10-31 11:33:50

+0

我已經添加了該行,它仍然返回一個沒有錯誤報告的空白頁面。 – Tijger 2014-10-31 11:41:59