I am new to coding. I am following a tutorial on how to create a PHP MySQL Ajax Live Search by Tutorial Public (linked here).
In the original tutorial, only one term per SQL table row can be searched (called "name" in the tutorial). In my own SQL table, I have multiple synonymous search names per row. One is called "name1", and the others are "name2" and "name3" respectively. https://imgur.com/a/WEEDsVx
Can anyone help me figure out how to edit the code in the tutorial so that the same row is able to be found by typing the result of "name1", "name2", or "name3"? Right now, only "name1" is searchable because the tutorial only provides an example of searching by one variable.
I have tried changing
$sql = "SELECT * FROM geneticconditions WHERE name1 LIKE ?";
to
$sql = "SELECT * FROM geneticconditions WHERE name1 LIKE ? OR name2 LIKE ? OR name3 LIKE ?";
but I get the error
Fatal error: Uncaught ArgumentCountError: The number of variables must match the number of parameters in the prepared statement in C:\xampp\htdocs\backend-search.php:17 Stack trace: #0 C:\xampp\htdocs\backend-search.php(17): mysqli_stmt_bind_param(Object(mysqli_stmt), 's', NULL) #1 {main} thrown in C:\xampp\htdocs\backend-search.php on line 17Fatal error: Uncaught ArgumentCountError: The number of variables must match the number of parameters in the prepared statement in C:\xampp\htdocs\backend-search.php:17 Stack trace: #0 C:\xampp\htdocs\backend-search.php(17): mysqli_stmt_bind_param(Object(mysqli_stmt), 's', NULL) #1 {main} thrown in C:\xampp\htdocs\backend-search.php on line 17
when I try that.
I think it could be due to the set parameters portion of the code
$param_term = $_REQUEST["term"] . '%';
Instead of ["term"], can I use ["name1", "name2", "name3"] to search by all 3 variables?
Code from the tutorial called "backend-search.php" below:
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$conn = mysqli_connect("localhost", "root", "", "example1");
// Check connection
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
if(isset($_REQUEST["term"])){
// Prepare a select statement
$sql = "SELECT * FROM geneticconditions WHERE name1 LIKE ?";
if($stmt = mysqli_prepare($conn, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_term);
// Set parameters
$param_term = $_REQUEST["term"] . '%';
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
// Check number of rows in the result set
if(mysqli_num_rows($result) > 0){
// Fetch result rows as an associative array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<p>" . $row["name1"] . "</p>";
}
} else{
echo "<p>No matches found</p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// close connection
mysqli_close($conn);
?>