我目前正在研究生院的一个项目。我正在构建一个可写和可搜索的数据库。我已经完成了可写的方面,但是我遇到了搜索功能的问题。问题是我无法在数据库中搜索和显示单个行,只能搜索和显示整个数据库,如果数据库有20k项且您只需要20,那么说实话,功能不是很有用。
我正在完整地复制下面的代码。
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_NOTICE);
// --------------------------------------------------------
/* THIS DEMO FILE ACCEPTS DATA FROM A WEB FORM
AND SAVES THE DATA TO A FLAT FILE AND THEN TO A DATABASE.
AS A CHECK WE READ THE DATA BACK FROM THE TABLE AND FROM
THE FILE.
Created by: PATRICK DUFF, based on example written by G. Benoit, on 2/28/15
Modified on: 3/1/15, 3/5/15/ 3/13/15
Contact: xxxxxxx
*/
// --------------------------------------------------------
/* GETTING READY FOR THE DATA */
/* the file to hold the data */
$filename = "testData.txt";
$status = "";
/* name of the database, table, username, password, table */
$hostname = "xxxxxxx"; // change this vars for your own db
$username = "xxxxx"; // whatever your student email user name is
$password = "xxxxx"; // your usual password
$dbname = "xxxxxx"; // this database name is assigned for you by the Lab Staff Lab Staff
$table = "xxxxxx"; // once you’re using your DB, you can create your own tables
//getting data from webfrom
$isbn = $_POST["isbn"];
$lname = $_POST["lname"];
$fname = $_POST["fname"];
$title = $_POST["title"];
$publisher = $_POST["publisher"];
$genre = $_POST["genre"];
$con = mysqli_connect("$hostname", "$username", "$password", "$dbname");
$isbn = $_POST["isbn"];
$lname = $_POST["lname"];
$fname = $_POST["fname"];
$title = $_POST["title"];
$publisher = $_POST["publisher"];
$genre = $_POST["genre"];
$con = mysqli_connect("$hostname", "$username", "$password", "$dbname");
//retrieving data from flat form & database.
echo "<!DOCTYPE html><html><head> <title> Record Search </title></head>";
echo "<body> Here is the data: The file contents of 'testData.txt so far is/are: ";
echo file_get_contents($filename);
echo "<hr /> End of reading file. Attempting to read database <hr />";
//creating searchability
$sql= "SELECT * FROM myLibrary WHERE isbn LIKE '%" .$isbn." %' OR fname LIKE '%" .$fname." %' OR lname LIKE'%" .$lname." %' OR title LIKE '%" .$title." %' OR publisher LIKE '%" .$publisher." %' OR genre LIKE'%" .$genre."%' ORDER BY lname" or
die(mysql_error());
$result = mysqli_query($con, $sql);
//displays result in table
if ($result->num_rows > 0) {
echo "<table id='myLibrary'><tr><th><u>ISBN</u></th><th> <u>First Name</u></th><th><u> Last Name </u></th><th><u> Title</u> </th><th><u>Publisher</u></th> <th><u>Genre</u></th></tr>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row["isbn"]."</td><td>". $row["fname"]."</td><td>" . $row["lname"]. "</td><td>" .$row["title"]. "</td><td>" .$row["publisher"]. "</td><td>".$row["genre"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
echo "End of Results";
出于某种原因,当我注释掉表格时,表格中没有任何内容显示出来。
我希望这很清楚,这是我第一次在这里发帖。
答案 0 :(得分:0)
您只需要限制查询以避免返回所有结果。
$limit = 10;
$sql= "SELECT * FROM myLibrary WHERE isbn LIKE '%" .$isbn." %' OR fname LIKE '%" .$fname." %' OR lname LIKE'%" .$lname." %' OR title LIKE '%" .$title." %' OR publisher LIKE '%" .$publisher." %' OR genre LIKE'%" .$genre."%' ORDER BY lname LIMIT " .$limit . ""