创建一个简单的数据库PHP搜索引擎

时间:2016-06-01 19:53:37

标签: php mysql search-engine

我尝试在PHP上创建一个简单的搜索引擎,它连接到数据库并从表属性中搜索字符串。并且基于关键字,搜索结果将显示具有相同关键字字符串的表行。我使用的是PHP 5.6.21版本

的search.php

<html>
<body>
 <form action="" method="get"><center>
	Search: <input type="text" name="search" placeholder="Enter any keywords" /><br><br>
		    <input type="submit" name="submit" value="Submit" />
			<center>
			 </form>
			 <hr>
<?php

$hostid= 'localhost';
$user='root';
$pass='';
$db= 'projectdata';
mysqli_connect($hostid, $user, $pass);
mysql_select_db($db);


 if(isset($_GET['submit'])){
	 
				$search_value=$_GET['search'];
				$query="SELECT * FROM Book where title LIKE '%$search_value%'";
				
				$run=mysql_query($query);
				
				if($run===FALSE){
					die(mysql_error());
				}
				while ($row=mysql_fetch_array($run))
				{
					$book_id=$row['book_id'];
					$title=$row['title'];
					$date_purchased=$row['date_purchased'];
					$loan_status=$row['loan_status'];
					echo "<table><tr><th>Book ID</th><th>Title</th><th>Date Purchased</th><th>Loan Status</th>";
					echo "<tr><td>$book_id</td><td>$title</td><td>$date_purchased</td><td>$loan_status</td></tr>";
				}
			}
?>
</body>
</html>

然而,它没有返回任何内容并且没有错误。以下是projectdata.php

<?php


   $hostid= 'localhost';          
   $user='root';
   $pass='';
   $db= 'projectdata';
   $link = mysqli_connect($hostid, $user, $pass, $db);
   if (!$link) {
   die('Connect Error (' .mysqli_connect_errno(). ') '     .mysqli_connect_error());
   }
   echo 'Success... ' .mysqli_get_host_info($link). "\n";

    $query8 = "CREATE TABLE Book(book_id int, title varchar(100), date_purchased     TIMESTAMP, loan_status varchar(10))";
   $query9 = "INSERT INTO Book VALUES(1023, 'The Prisoner of Zenda', '2008-7-04', 'Available'),
                                 (2311, 'Rupert of Hentzau','2001-7-10', 'Available'),
                                 (7854, 'Management of Information System', '2010-6-14', 'Available'),
                                 (4507, 'Introduction to C++', '2012-1-19', 'Available'),
                                 (5319, 'Computer Networking 13th Edition', '2013-5-29', 'Available'),
                                 (3076, 'Introduction to Web Programming', '2008-4-26', 'Available'),
                                 (6901, 'Foundation of Quantum Mechanics', '2008-7-04', 'Available')";


  if (mysqli_query($link,$query8) ===TRUE)
 {
   echo ("Table Book successfully created.<br>");
   }


  if (mysqli_query($link,$query9) ===TRUE)
 {
   echo ("Values have been inserted into Book.<br><br>");
   }

 mysqli_close($link);

?>

0 个答案:

没有答案