0
我有一個表格,其中用戶可以把字符串搜索數據庫嘗試使用表單搜索表中的字符串?
的index.php
<html>
<head>
<title>Search the Database</title>
</head>
<body>
<form action="search.php" method="post">
Search: <input type="text" name="search" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html
在這裏,我搜索數據庫與查詢
的search.php
<?php
mysql_connect ("localhost", "db","pw") or die (mysql_error());
mysql_select_db ("stories"); //This selects the specific database you want your results to come from (as you may have more than one database). Change "test" to your database name.
$search = $_POST['search']; //This grabs everything your user has put into the search field (which has been labelled as "search" earlier from the .HTML page).
$sql = mysql_query("select * from stories where stories like '%$term%'"); //Assigns a variable called "$sql" to the result of your "search". And the use of LIKE allows you to only have to type in (for example if you were searching a name).. Jord instead of the full Jordan.
while ($row = mysql_fetch_array($sql)){ //We start a "while" loop so you can output multiple results. Also we assign an array "$row" to each database table column you would like to display. Below is an example if you wanted multiple tables to be searched through and displayed if a match is found.
echo '<br/> Result '.$row['stories'];
}
?>
我沒有得到任何錯誤,但我也沒有得到任何輸出。查詢有問題嗎?
'$ term'應該是您的查詢中的'$ search'。 – Rikesh
什麼是$ term這裏? – Jenz