Rouge
Rouge

Reputation: 4239

How to return DB data efficiently

I am trying to pass a big chunk of data via ajax call from the server.

I have

foreach ($results as $field){

    $data[]=$fieldName=array('ID'=> $field['ID'], 'Text'=> $field['Text']...and so much more);

}

I need to show the field name (ID) and the data ($field['ID']). Are there faster way to do this without adding so many more fields manually in my array? Thanks a lot!

Upvotes: 0

Views: 45

Answers (2)

Matt S
Matt S

Reputation: 15374

Isn't that equivalent to:

foreach ($results as $field){
    $data[] = $field;
}

If it's an AJAX request expecting JSON, then just encode the whole thing in one function call:

json_encode($results);

Upvotes: 0

Marc B
Marc B

Reputation: 360792

You could select only the fields you really do require in your query, e.g.

SELECT field1, field2, field3 ...

instead of

SELECT *

Then you can simply do

while($row = fetch_from_db($result)) {
   $data[] = $row;
}

Upvotes: 1

Related Questions