0

So the problem that I am encountering is that I have a select field that is populated via PHP. A MySQL DB is queried and the PHP processes the returned array, printing it into the select field's options.

However, when a single result is returned from the DB, only the first character of the string is displayed. If multiple results are returned, then all results display fully. I have checked that when a single result is returned, that the string itself is correct, which it is, so I don't believe this to be an issue with the data retrieval. Furthermore, no errors are generated either, so no clues are provided as to where things are going wrong.

I've tried to find other StackOverflow posts describing this issue, but haven't found anything, nor can I find anything elsewhere on the internet.

This is the DB Stored Procedure that collects the results:

BEGIN
SELECT BranchID, City FROM Branch;
END

Then this PHP turns that data into an associative array:

$result = $currentconnection->DBQuery("CALL Fetch_Branch_City_All()");
        if( !$result) die($mysql->error);
        while($row = $result->fetch_assoc()){
            $BranchID = $row["BranchID"];
            $CityName = $row["City"];
            $results = $this->array_push_assoc($results, 'BranchID', $BranchID);
            $results = $this->array_push_assoc($results, 'CityName', $CityName);
        }

This is a go-between piece of code that calls the function above and makes it available to the code below, via the $Query variable.

$Query = $pModel->fetchBranchCity();

And finally this code creates the select from the array data:

<?php //Note if only one branch in system, displays single character
                extract($Query, EXTR_OVERWRITE);

                for ($a = 0; $a < count($BranchID); ++$a) {
                    if($BranchID[$a] == $BranchIDText){ ?>
                        <option value="<?=$BranchID[$a]?>" selected="selected"> <?=$CityName[$a]?> </option>
                    <?php }else{ ?>
                        <option value="<?=$BranchID[$a]?>"> <?=$CityName[$a]?> </option> 
                    <?php }
                }
                ?>
4
  • 1
    what is $Query? where is the relationship between $result and $Query? An extra point: Why are you using two variables $a and $b? You only need one of them...simpler and more readable. Commented Jan 1, 2018 at 6:58
  • @C.Geek I've added the code that explains the $Query variable. Thank you for your suggestion as well, I've changed the code accordingly. Commented Jan 2, 2018 at 0:14
  • $result is a string when one result and array if many. You should either make if(is_array) or refactor the function to return consistent results.. is that some framework? Commented Jan 2, 2018 at 0:32
  • @Mat thank you for your comment. I've double checked and $result is always an array, regardless of if it's a singular result or multiple. It's a custom MVC application. Commented Jan 2, 2018 at 1:02

1 Answer 1

1

It has to do with the extract, while the $result is always an array, the $BranchID and $CityName aren't always.

So when they have one value, they aren't arrays, just strings and looping through them extracts the characters instead of the whole string from an array.

Please do tell me if you need help handling this exception in code.

hints: is_array

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

1 Comment

Thank you so much @C. Geek - you were spot on with the variables inside the array not being arrays themselves when a single result was being returned. I added in an is_array check as suggested and the code works perfectly :) Thanks again

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.