I am trying to search 2 columns in the same table with a like statement. I am new to using PDO.
It is searching and returning the result as intended but it only seems to be searching the "courseTitle" column. The search seems to be ignoring the "longTitle" column.
My example makes sense to me but doesn't work. What am I missing here?
try{
if(isset($_REQUEST["term"])){
// create prepared statement
$sql = "SELECT * FROM courseCodes WHERE courseTitle LIKE :term OR longTitle LIKE :term";
$stmt = $pdo->prepare($sql);
$term = $_REQUEST["term"] . '%';
// bind parameters to statement
$stmt->bindParam(":term", $term);
// execute the prepared statement
$stmt->execute();
if($stmt->rowCount() > 0){
while($row = $stmt->fetch()){
echo "
<tr onclick='selectRow(this)'>
<td>".$row['courseNumber']."</td>
<td>".$row['courseTitle']."</td>
<td>".$row['longTitle']."</td>
<td><input type='checkbox' id='".$row['id']."' name='courseNumber[]' value='".$row['courseNumber']."'></td>
</tr>
";
}
} else{
echo "<p>No matches found</p>";
}
}
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
// Close statement
unset($stmt);
// Close connection
unset($pdo);
I MADE THESE CHANGES AND NOTHING IS RETURNED ... UUGGGGG
$sql = "SELECT * FROM courseCodes WHERE courseTitle LIKE :term OR longTitle LIKE :term2"; // <--- ADDED term2
$stmt = $pdo->prepare($sql);
$term = $_REQUEST["term"] . '%';
// bind parameters to statement
$stmt->bindParam(":term", $term);
$stmt->bindParam(":term2", $term); // <--- ADDED
// execute the prepared statement
$stmt->execute();
if($stmt->rowCount() > 0){