2014-04-17 102 views
0

我試圖創建一個搜索引擎,將從mySQL數據庫中提取信息。我的代碼如下:WAMP服務器不允許php搜索引擎?

<!doctype html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Search Engine Test</title> 
</head> 
<body> 
<script language="php"> 
// Create a database connection 

$connection = mysql_connect("localhost", "root", "", "test"); 
if (!connection) { 
    die("Please reload page. Database connection failed: " . mysql_error()); 
} 

// Select a databse to use 
$db_select = mysql_select_db("test", $connection); 
if (!$db_select) { 
    die("Please reload page. Database selection failed: " . mysql_error()); 
} 

// Search Engine 
// Only execute when button is pressed 
if (isset($_POST['keyword'])) { 
    // Filter 
    $keyword = trim($_POST['keyword']); 

    // Select statement 
    $search = "SELECT Price FROM 'table_1' WHERE * LIKE '%$keyword%'"; 
    // Display 
    $result = mysql_query($search) or die('query did not work'); 

    while ($result_arr = mysql_fetch_array($result)) { 
     echo $result_arr['*']; 
     echo " "; 
     echo "<br>"; 
     echo "<br>"; 
    } 

    $anymatches = mysql_num_rows($result); 
    if ($anymatches == 0) { 
     echo "Nothing was found that matched your query.<br><br>"; 
    } 
} 
</script> 

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> 
    <input type="text" name="keyword"> 
    <input type="submit" name="search" value="Search"> 

但是,當我運行它時,我收到一條通知,說我被禁止訪問服務器。任何幫助,將不勝感激。

+0

你能在這裏粘貼錯誤嗎? – Ibu

+0

可能是一個httpd.conf問題。 – dnelson

+0

'wwhile'應該是'while'。我已經修復它假設它只是一個錯字。 – Tiny

回答

0

$search = "SELECT Price FROM table_1 WHERE fieldname LIKE '%$keyword%'";

不能使用單引號括起來的字段名稱,因爲這是字符串是如何定義的。如果你的表名是一個保留字,你可以使用重鍵(`)。你也不能做WHERE *,你需要指定一個字段來比較關鍵字。

我也建議不要在PHP中使用mysql命令,因爲它們已被棄用。使用更新的mysqli命令