1

I have some functions like this:

function doLegends($in_result)
{
    global $legend_table;
    while ($temp_row = mysqli_fetch_row($in_result))
    {
        $legend_table[] = $temp_row;
    }
}

They add each row one-by-one into a PHP array. Is there a way to save the resultset as an array in one single step without needing to get each row individually? Also, is there a performance benefit? Thanks.

[edit]

Here is the query.

SELECT  l.legend_group, l.legend_description
FROM    legends as l
WHERE   l.record_id = in_record_id;

record_id is not unique or a primary key, so it returns multiple rows.

[edit]

Is this the correct syntax?

function doLegends($in_result)
{
    global $legend_table;
    $legend_table = mysqli_fetch_array($in_result);
}

I get an syntax error, unexpected '}' error at the last line.

[edit]

Okay, the syntax error was a false positive. However, now the mysqli_fetch_array function is only returning a subset of the expected result. For instance, $legend_table should have 6 rows, but the function is only returning 2 rows. There are gaps in the SQL IDs. Some numbers are skipped due to having deleted some rows. Can this have an effect?

[edit]

Here are the contents of my SQL table:

legend_id,record_id,legend_group,legend_description
896,180,1,"Unit Actions"
897,180,2,Lenses
898,180,4,Multiplayer
899,180,5,Camera
900,180,6,"Game Screens"
901,180,8,"Game Commands"

But when I count the number of rows using this:

error_log("woot " . count($legend_table));

the error log only shows woot 4 instead of woot 6.

6
  • Can you show us the query you are running? You might be able to do an aggregation across records. Commented Jan 6, 2017 at 7:39
  • 2
    if you were using PDO you could just do fetchAll() Commented Jan 6, 2017 at 7:40
  • I added the query to my post. And I don't know what a PDO is. Commented Jan 6, 2017 at 7:44
  • 1
    You can make use of mysqli_fetch_array to get the result in array format or mysqli_fetch_assoc to get the result in associative array format Commented Jan 6, 2017 at 7:54
  • 1
    You 'should' have a look at PDO if you don't yet know what it is. You could start here and work your way up. Your problem of getting all results as an array for example, would work with the fetchAll() function, see here. Commented Jan 6, 2017 at 8:24

1 Answer 1

3

You can make use of mysqli_fetch_array to get the result in array format or mysqli_fetch_assoc to get the result in associative array format

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

8 Comments

Might help if you give the OP examples using those functions. Maybe also include links to php.net where OP can have a look at the functions with php.net examples, like so: mysqli_fetch_array mysqli_fetch_assoc
Can you post the code where the issue is occurring? mysqli_fetch_array($in_result) is correctly used.
Well, I'm not sure. Can you give some examples?
Use print_r($legend_table) to get the contents of the array. Post the result.
This shows only one record in the array. But you have mentioned in the post that the count is showing as 4. Can you show the query that is run? And I don't see any spaces in the legend_id row values.
|

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.