I have a search.php page that successfully searches a database called “sixcols” and displays the correct number of results. But suddenly, it only displays information from the first column of each result. No matter which of the six types of columns I list first in the code, only that column appears. I have no idea why all 6 aren’t appearing.
I know mysql_fetch_array is becoming depracated, but I swear this worked this morning and I don’t know what I changed.
I’ve searched for 2 hours for a typo or an explanation and I can’t figure it out. Any help is much appreciated!
Here’s the code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
$searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
$sqlCommand = "SELECT Subcategory, Type, Author, Summary, Date, Source FROM sixcols WHERE Author LIKE '%$searchquery%' OR Summary LIKE '%$searchquery%'";
include_once("connect.php");
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />";
while($row = mysql_fetch_array($query)){
$Subcategory = $row["Subcategory"];
$Type = $row["Type"];
$Author = $row["Author"];
$Summary = $row["Summary"];
$Date = $row["Date"];
$Source = $row["Source"];
$search_output .= '<br/> <span class="subcategory">' . $Subcategory . '</span>'; '<span class="type">' . $Type . '</span>'; '<span class="author">' . $Author . '</span>'; '<span class="summary">' . $Summary . '</span>'; '<br/> <span class="date">' . $Date . '</span>'; '<span class="source">' . $Source . '</span>';
} // close while
} else {
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
}
}
?>
You have a syntax issue where you stop appending to
$search_output. You should turn error reporting on to catch these errors.You also need to stop using
mysql_functions. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi – this article will help you decide which.