Reputation: 57
I have looked through the posts here on SO but cannot find anything that seems to address my problem. I am fairly new to PHP/MySQL, and I cannot seem to figure out why this array_merge_recursive function keeps telling me that my first array is not an array. I have tested it and everything tells me it is… until it hits this function. Here is my code. I am trying to process multiple optional variables from a search form (I have looked into Sphinx/Lucene but am unable to use them at this time). First, I am processing some variables that have been POSTed to the page.
if (isset($_POST['city'])) {
$search = mysqli_real_escape_string($db, $_POST['city']);
$sqlCity = "SELECT `user_id` FROM `clients` WHERE MATCH (`city`) AGAINST ('$search' IN NATURAL LANGUAGE MODE)";
$dsCity = $db->query($sqlCity);
}
if (isset($_POST['state'])) {
$search = mysqli_real_escape_string($db, $_POST['state']);
$sqlState = "SELECT `user_id` FROM `clients` WHERE `state` = $search";
$dsState = $db->query($sqlState);
}
if (isset($_POST['zip'])) {
$search = mysqli_real_escape_string($db, $_POST['zip']);
$sqlZip = "SELECT `user_id` FROM `clients` WHERE `zip` = $search";
$dsZip = $db->query($sqlZip);
}
if (isset($_POST['business-name'])) {
$search = mysqli_real_escape_string($db, $_POST['business-name']);
$sqlBus = "SELECT `user_id` FROM `clients` WHERE `business_name` = '$search' ";
$dsBus = $db->query($sqlBus);
}
Then, I want to merge the resulting arrays into one array that gets passed as a single variable to the rest of the PHP code.
// Create array from Detailed Search variables
if (isset($_POST['city']) || isset($_POST['state']) || isset($_POST['zip']) || isset($_POST['business-name']) ) {
$searchResults = array_merge_recursive($dsCity, $dsState, $dsZip, $dsBus);
}
?>
[Breaks out into some HTML here… and then gets processed. The rest of the code works. I keep getting the error before it even gets this far.]
while ($row=$searchResults->fetch_object()) {
$userID = $row->user_id;
The error message I keep getting says "array_merge_recursive(): Argument #1 is not an array". Any ideas?
Upvotes: 0
Views: 696
Reputation: 4302
You need to fetch the result back from your query. mysqli::query returns a mysqli_result object, not an array.
$result = $db->query($sqlZip);
$dsZip = $result->fetch_array();
Do that for each of your queries. This is the type of object you are trying to use: http://php.net/manual/en/class.mysqli-result.php
I hope that helps!
Upvotes: 2
Reputation: 533
You should initializite all arrays that you use in array_merge_recursive, cause they can not exist in your code.
Upvotes: -1