0

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?

2 Answers 2

2

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!

Sign up to request clarification or add additional context in comments.

3 Comments

Took a little tweaking, but this worked for me, thank you. Apparently I also needed to put single quotes around the $variables in my SQL queries...? I haven't had that issue before, but I eradicated an error by making that change. Appreciate the help.
Incidentally, how do I retrieve the contents of the new merged array I have created? When it gets to the final code I provided above: while ($row=$searchResults->fetch_object()) { $userID = $row->user_id; it gives me this error: Call to a member function fetch_object() on array.
BTW, the page you shared says that mysqli_result can be an array. That was why I was using it. I am sure I am missing something. Can you expound?
-1

You should initializite all arrays that you use in array_merge_recursive, cause they can not exist in your code.

2 Comments

You are correct, there is the possibility that some of these arrays will be empty. I tried initializing them as empty but that didn't seem to work. Instead, I took advice from another SO post and did this: if (isset($_POST['city']) || isset($_POST['state']) || isset($_POST['zip']) || isset($_POST['business-name']) ) { $searchResults = array_merge_recursive((array)$dsCity, (array)$dsState, (array)$dsZip, (array)$dsBus); }
Apparently that forces them to be seen as arrays even when they are empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.