• Main navigation
登入區塊
帳號:

密碼:

記住我



忘記密碼?

現在註冊!
網站資訊區塊
站務管理者

kiang
 

tokimeki
 

sam0228
 

morris
 

shiang
 

SoltyRain
 

廣告





Little Searching engine
Just popping in
註冊日期:
2007/3/18 19:30
文章: 2
hi guys,

trying to write a searching engine with php and mysql. my question is that as the an input keyed in the web site, the php tell mysql to search for it.

here is my code:


$sql = "SELECT * FROM `General` WHERE `Item_Name` LIKE $Name";
$conn=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password );
$result=mysql_db_query( $mysql_database, $sql,$conn );
while($row=mysql_fetch_row($result))
{
print_r($row);
}
mysql_free_result($result);

as the users will not type exact name of looking for item, so i would prefer to use LIKE instead of = sign.

somehow i always get error messages:

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\Book_Shop\SearchOK.php on line 36

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\Book_Shop\SearchOK.php on line 47

please help. thank you very much.

發表日期:2007/5/9 1:39
應用擴展 工具箱


回覆: Little Searching engine
網站管理員
註冊日期:
2005/9/8 17:37
文章: 691
你的$sql語法有問題所以無法得到正確的$result
可以echo $sql看看
我猜是在
LIKE $Name這裡
$Name對應的應該是一個varchar或char的欄位
改成
"SELECT * FROM `General` WHERE `Item_Name` LIKE "'" . $Name . "'";
另外
你使用了like卻沒加%
這樣的用法似乎不是很適用

發表日期:2007/5/9 10:20
應用擴展 工具箱


回覆: Little Searching engine
Just can't stay away
註冊日期:
2006/12/26 8:48
文章: 144
SQL syntax error.

1. The field-name should be put in double-quote. It means case-sensitive.

2. The LIKE only applies for text-based type field, therefore you need to put content in single-quote. Besides, LIKE must be used with '%'.

In your case:

$sql 
"SELECT * FROM "General" WHERE "Item_Name" LIKE '%{$Name}%'; ";


SQL語法錯誤:
1.表格及欄位名稱應該以雙引號括起。這表示區分大小寫。若不加雙引號,一律作小寫處理。

2.LIKE 只適用於文字型態的欄位,所以必須將查詢內容以單引號括起。此外,LIKE的查詢內容必須配合 % 使用。

發表日期:2007/5/9 16:48
應用擴展 工具箱







[進階搜尋]