I am upgrading my code from mysql to mysqli, but I have come across something I don't know how to do and Google isn't much help on this one... Basically I have a search form on my site which searches mysql based on what is completed. I build the sql query using something like this...
$query = "SELECT COUNT(profile) FROM profiles WHERE profile like '%'"; // << This will always return everything
if(isset($_POST['minage'])) {
$query .= " AND minage >= " . $_POST['minage'];
}
if(isset($_POST['maxage'])) {
$query .= " AND maxage >= " . $_POST['maxage'];
}
$result = mysql_query($query);
Before you jump on me, please bear in mind that is just a quick example, I wouldn't take post variables directly into SQL :/
So how do I do this in mysqli using a mysql_stmt_prepare? Also just like to add there are about 28 WHERE clauses in my query, but I have only used 2 as an example...
This would be my new query
$stmt = mysqli_prepare($sql_connect, "SELECT COUNT(profile) FROM profiles WHERE age >= ? AND age <= ?");
mysqli_stmt_bind_param($stmt, "ss", $_POST['minage'], $_POST['maxage']);
mysqli_stmt_execute($stmt);
The only way I can think of [and I don't know if this will work] is like this... ???
if (!isset($_POST['minage'])) {
$minage = '%';
}