2014-10-31 207 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