save resources *sql_query and array size

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Asken

    save resources *sql_query and array size

    Hello,

    i could not find information about extracting data by using
    *sql_query (for me mssql_query) and do that in a way that saves server
    memory.

    I usually use:

    $sql = "exec sp_get_Gremiums Mitglieder
    @foo = 'myFoo',
    @bar = 42";

    $rsResult = mssql_query($sq l);
    $i=0;
    while($data = mssql_fetch_arr ay($rsResult))
    {
    extract($data);
    $mitglieder[$i] = array("id" => $id, "displayNam e" => $displayName,
    "eMail" => $eMail, "authuser" =>
    $authuser);
    $i+=1;
    }

    So the result is an array half the size; instead of $mitglieder[0] and
    $mitglieder[id] there is just $mitglieder[id].

    Is there a better way. How can i free memory for Variables that are no
    longer used (sql, rsResult, i). I sometimes read something about die()
    / exit() but the online documentation on php.net did not say anything
    that would led to belive that memory is freed.

    Is there a necessity / advantage to close the connection to
    sql-server?


    Thanks a lot

    Tom Asken
  • Alvaro G Vicario

    #2
    Re: save resources *sql_query and array size

    *** Tom Asken wrote/escribió (27 Apr 2004 03:48:42 -0700):[color=blue]
    > while($data = mssql_fetch_arr ay($rsResult))
    > {
    > extract($data);
    > $mitglieder[$i] = array("id" => $id, "displayNam e" => $displayName,
    > "eMail" => $eMail, "authuser" =>
    > $authuser);
    > $i+=1;
    > }
    >
    > So the result is an array half the size; instead of $mitglieder[0] and
    > $mitglieder[id] there is just $mitglieder[id].[/color]

    You have just reinvented mysql_fetch_ass oc(), haven't you?

    I guess the best way to save memory is using data while reading it and
    afterwards unsetting results. Your code makes a copy of the complete
    results so you actually get data twice in memory.
    [color=blue]
    > Is there a better way. How can i free memory for Variables that are no
    > longer used (sql, rsResult, i).[/color]

    Maybe unset()
    [color=blue]
    > I sometimes read something about die()
    > / exit() but the online documentation on php.net did not say anything
    > that would led to belive that memory is freed.[/color]

    With die and exit the execution of the script is terminated. If that
    doesn't release memory then I don't think you could blame your script.

    --
    --
    -- Álvaro G. Vicario - Burgos, Spain
    --

    Comment

    • Chris Hope

      #3
      Re: save resources *sql_query and array size

      Alvaro G Vicario wrote:
      [color=blue]
      > *** Tom Asken wrote/escribió (27 Apr 2004 03:48:42 -0700):[color=green]
      >> while($data = mssql_fetch_arr ay($rsResult))
      >> {
      >>extract($data );
      >>$mitglieder[$i] = array("id" => $id, "displayNam e" => $displayName,
      >>"eMail" => $eMail, "authuser" =>
      >> $authuser);
      >> $i+=1;
      >> }
      >>
      >> So the result is an array half the size; instead of $mitglieder[0] and
      >> $mitglieder[id] there is just $mitglieder[id].[/color]
      >
      > You have just reinvented mysql_fetch_ass oc(), haven't you?[/color]

      That would be mssql_fetch_ass oc() for mssql :)

      Reimplementing your code using this function would look like so:

      while($data = mssql_fetch_ass oc($rsResult))
      {
      $mitglieder[$i] = $data;
      $i++;
      }

      and the net result is the same as your original code but quite a lot
      cleaner, and using less resources.

      --
      Chris Hope
      The Electric Toolbox - http://www.electrictoolbox.com/

      Comment

      Working...