Little Searching engine

edited 十月 2013 in 資料庫
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.

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=2609

評論

  • edited 五月 2007
    你的$sql語法有問題所以無法得到正確的$result
    可以echo $sql看看
    我猜是在
    LIKE $Name這裡
    $Name對應的應該是一個varchar或char的欄位
    改成
    "SELECT * FROM `General` WHERE `Item_Name` LIKE "'" . $Name . "'";
    另外
    你使用了like卻沒加%
    這樣的用法似乎不是很適用
  • edited 五月 2007
    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的查詢內容必須配合 % 使用。
Sign In or Register to comment.