我的搜索功能
<?php
$i=0;
$column_name = 'title'; // column to search by
$k =$_GET['k'];
$terms = explode(" ",$k);
//connect before calling mysql_real_escape_string
mysql_connect("localhost","","");
mysql_select_db("test");
$query ="SELECT bookid,title,author
FROM books WHERE";
foreach ($terms as $each){
$i++;
$each = '%' . $each . '%'; // add wildcard
$each = mysql_real_escape_string($each); // prevent sql injection
if($i==1)
$query .= " $column_name LIKE '$each' ";
else
$query .= " OR $column_name LIKE '$each' ";
}
echo 'QUERY: ' . $query;
$query = mysql_query($query) OR DIE(mysql_error());
//Code below is for using the relationships table assuming you have a column name id that
//references to the relationships table. Also, you should add a index on the column id.
$results = "";
while($row = mysql_fetch_array($query)) {
$results .= '<li>
<a href="book-relationships.php?id='.$row['id'].'">'.$row['title'].' author: '.$row['author'].'</a>
</li>';
}
$results = '<ul>' . $results . '</ul>';
echo $results;
我需要帮助在搜索时将一个数据库与另一个链接起来,这个数据库列出了所有在搜索中出现的书籍,然后将它们与另一个名为关系的表进行比较,并打印出与搜索的书籍有关系的书籍的标题。
当搜索运行时,这是在浏览器中产生的
QUERY: SELECT id,title,author FROM books WHERE title LIKE '%jarrads%' Unknown column 'id' in 'field list'
任何帮助将不胜感激我 100% 被卡住了
图式
书籍 - bookid、title、author、yearpublished、publisher..... 关系 - relationshipid、bookOne、bookTwo、关系、relationshiplikes、relationshipdislikes
bookOne 和 bookTwo,与 bookid 相关