I need help creating a function in PHP that accepts optional variables. So far this is what I have done:
function customFunc($var, $var1){
global $conn;
$query = "SELECT * FROM table WHERE col = '{$var}' AND col1 = '{$var1}';";
$result = mysqli_query($conn, $query);
return $result;
}
If I call the function without supplying $var1
, MySQL does not return any rows, even though we have some.
Now what I also need to do is select all from col1 if $var1
is not set. I would also like to scale up the function and include more optional variables $var2
, $var3
, etc like so:
function customFunc($var, $var1, $var2, $var3){
global $conn;
$query = "SELECT * FROM table WHERE col = '{$var}' AND col1 = '{$var1}' AND col2 = '{$var2}' AND col3 = $var3;";
$result = mysqli_query($conn, $query);
return $result;
}