我试图将它组织成一个有自己列的表格,但每次我尝试时,我都会得到一些非常混乱或完全错误的东西。如果我能从 SQL 中获得一些帮助,那就太好了。例如看这张图片:
http://bfast.elementfx.com/design/table.JPG
这是代码
$dbHost = 'localhost'; // localhost will be used in most cases
$dbUser = 'root';
$dbPass = 'root';
$dbDatabase = 'root'; // the database you put the table into.
$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());
mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());
$error = array();
$results = array();
if (isset($_GET['search'])) {
$searchTerms = trim($_GET['search']);
$searchTerms = strip_tags($searchTerms); // remove any html/javascript.
if (strlen($searchTerms) < 2) {
$error[] = "Search terms must be longer than 2 characters.";
}else {
$searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
}
// If there are no errors, lets get the search going.
if (count($error) < 1) {
$searchSQL = "SELECT brandname, manufacturer, distributor, modelnumber, date, expirey FROM productlist WHERE ";
// grab the search types.
$types = array();
$types[] = isset($_GET['Brand Name'])?"`brandname` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['Manufacturer'])?"`manufacturer` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['distributor'])?"`distributor` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['modelnumber'])?"`modelnumber` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['E'])?"`E` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['F'])?"`F` LIKE '%{$searchTermDB}%'":'';
$types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
if (count($types) < 1)
$types[] = "`brandname` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked
$andOr = isset($_GET['matchall'])?'AND':'OR';
$searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `brandname`"; // order by title.
$searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
if (mysql_num_rows($searchResult) < 1) {
$error[] = "The search term provided {$searchTerms} yielded no results.";
}else {
$results = array(); // the result array
$i = 1;
while ($row = mysql_fetch_assoc($searchResult)) {
$results[] = "{$i}: <b>Brand Name:</b> {$row['brandname']}<br /><b>Distributor:</b> {$row['distributor']}<br /><b>Manufacturer:</b> {$row['manufacturer']} <br /><b>Model Number:</b> {$row['modelnumber']}<br /><b>Certifying Agency:</b> {$row['E']} {$row['F']}<br /><br /><br />";
$i++;
}