我已经创建了一个使用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