2014-11-08 45 views
-2

我創建使用php and mysql,該搜索框,我已經創建index.phpsearch.php文件。錯誤在PHP代碼搜索框

,我已經創建了一個名爲作爲搜索數據庫。

這裏是我的代碼:

的index.php:

<html> 
<head> 
<title>Title of your search engine</title> 
</head> 
<body> 
<form action='search.php' method='GET'> 
<center> 
<h1>My Search Engine</h1> 
<input type='text' size='90' name='search'></br></br> 
<input type='submit' name='submit' value='Search source code' ></br></br></br> 
</center> 
</form> 
</body> 
</html> 

的search.php:

<?php 
$button = $_GET ['submit']; 
$search = $_GET ['search']; 
if(!$button) 
echo "you didn't submit a keyword"; 
else 
{ 
if(strlen($search)<=1) 
echo "Search term too short"; 
else{ 
echo "You searched for <b>$search</b> <hr size='1'></br>"; 
mysql_connect("localhost","root",""); 
mysql_select_db("search"); 
$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 searchengine WHERE $construct"; 
$run = mysql_query($construct); 
$foundnum = mysql_num_rows($run); 
if ($foundnum==0) 
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1. 
Try more general words. for example: If you want to search 'how to create a website' 
then use general keyword like 'create' 'website'</br>2. Try different words with similar 
meaning</br>3. Please check your spelling"; 
else 
{ 
echo "$foundnum results found !<p>"; 
while($runrows = mysql_fetch_assoc($run)) 
{ 
$title = $runrows ['title']; 
$desc = $runrows ['description']; 
$url = $runrows ['url']; 

echo " 
<a href='$url'><b>$title</b></a><br> 
$desc<br> 
<a href='$url'>$url</a><p> 
"; 
} 
} 
} 
} 
?> 

時候會運行這段代碼,它顯示了以下錯誤,

注意:未定義的變量:x在C:\ xampp \ htdocs \ selva \ search \ search.php上第21行

說明:未定義變量:構建體在C:\ XAMPP \ htdocs中\塞爾瓦\上線搜索\的search.php 23

警告:mysql_num_rows()預計參數1是資源,在布爾C中給出:\ XAMPP \ htdocs中\塞爾瓦\上線32

搜索\ search.php中任何人都可以幫助解決這個問題,在此先感謝。

+0

你在哪裏定義'$ x'和'$ construct'變量? – ekad 2014-11-08 09:58:09

+0

前兩個錯誤是不言而喻的:你應該跟蹤那些你自己。最後你可以在Google上輕鬆找到,但看看[這裏](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result -boole) – 2014-11-08 10:00:41

+0

@ekad:那該如何定義,? – deepika 2014-11-08 10:00:45

回答

0

嘗試,foreach循環之前定義

$x = 0; 
$construct = ''; 

的mysql_query()返回false如果查詢失敗。這就是爲什麼mysql_num_rows()會抱怨接收一個布爾值。

+0

我在問警告/ – deepika 2014-11-08 10:06:42

+0

ok @amal:如何解決這個問題? – deepika 2014-11-08 10:09:54

+0

@ Arif_suhail_123:幫我解決這個 – deepika 2014-11-08 10:12:32