Optimizing a function...

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

    Optimizing a function...

    All,
    This is a function that I use quite often. I pass in my SQL string and
    return the results as an array. Does anyone see any optimizations that I can
    make (eventually I would like to make a class, but I am still new).

    function table_array ($sql_string)
    {
    $result = mysql_query($sq l_string) or die("dead");
    $num_rows = mysql_num_rows( $result);
    $num_fields = mysql_num_field s($result);
    $j = 0;
    $x = 1;
    while ($row = mysql_fetch_arr ay($result)) {
    for($j = 0;$j < $num_fields;$j+ +) {
    $name = mysql_field_nam e($result, $j);
    $arr[$x][$name] = $row[$name];
    }
    $x++;
    }
    return array('arr' => $arr, 'rows' => $num_rows, 'fields' => $num_fields);
    }

    Always trying to shave off some access times =)
    Thanks.


  • neur0maniak

    #2
    Re: Optimizing a function...

    Here's a basic class I made a while ago that does something similar..

    Once on a page you'll need to do:
    $db = new dbObject("local host", "database", "myuser", "mypassword ");

    from then on, you can just use:
    $db->query("SELEC T * FROM wherever");

    It'll return just the array of contents of your query.

    Once you have the array, you can get the number of rows by:
    $num_rows = count($returned Array);

    and you can get the number of fields by:
    $num_fields = intval(count(@$ returnedArray[0]));

    The plan was back then that I could replace the class if I ever moved to
    a different database technology.

    <?php

    class dbObject {
    var $server = "localhost" ;
    var $database = "";
    var $username = "root";
    var $password = "";

    var $connection;

    function dbObject ($server, $database, $username, $password) {
    if ($this->connection = mysql_connect($ server, $username,
    $password)) {
    mysql_select_db ($database, $this->connection);
    }
    }

    function query($sql) {
    $dbO = mysql_query($sq l, $this->connection);
    if (strtolower(sub str($sql, 0, 6))=="select") {
    $ret = Array();
    if (!$dbO) {print "SQL: {$sql} <BR /> FAILED";}
    while ($dbR = mysql_fetch_arr ay($dbO)) {$ret[] = $dbR;}
    }else{
    $ret = $dbO;
    }
    return $ret;
    }

    function close() {
    mysql_close($th is->connection);
    }
    }

    ?>

    StinkFinger wrote:
    [color=blue]
    > All,
    > This is a function that I use quite often. I pass in my SQL string and
    > return the results as an array. Does anyone see any optimizations that I can
    > make (eventually I would like to make a class, but I am still new).
    >
    > function table_array ($sql_string)
    > {
    > $result = mysql_query($sq l_string) or die("dead");
    > $num_rows = mysql_num_rows( $result);
    > $num_fields = mysql_num_field s($result);
    > $j = 0;
    > $x = 1;
    > while ($row = mysql_fetch_arr ay($result)) {
    > for($j = 0;$j < $num_fields;$j+ +) {
    > $name = mysql_field_nam e($result, $j);
    > $arr[$x][$name] = $row[$name];
    > }
    > $x++;
    > }
    > return array('arr' => $arr, 'rows' => $num_rows, 'fields' => $num_fields);
    > }
    >
    > Always trying to shave off some access times =)
    > Thanks.
    >
    >[/color]

    Comment

    • StinkFinger

      #3
      Re: Optimizing a function...

      "neur0mania k" <usenet@neur0ma niak.co.uk> wrote in message
      news:40f896e8$0 $225$ed2619ec@p tn-nntp-reader03.plus.n et...[color=blue]
      > Here's a basic class I made a while ago that does something similar..
      >
      > Once on a page you'll need to do:
      > $db = new dbObject("local host", "database", "myuser", "mypassword ");
      >
      > from then on, you can just use:
      > $db->query("SELEC T * FROM wherever");
      >
      > It'll return just the array of contents of your query.
      >
      > Once you have the array, you can get the number of rows by:
      > $num_rows = count($returned Array);
      >
      > and you can get the number of fields by:
      > $num_fields = intval(count(@$ returnedArray[0]));
      >
      > The plan was back then that I could replace the class if I ever moved to a
      > different database technology.
      >
      > <?php
      >
      > class dbObject {
      > var $server = "localhost" ;
      > var $database = "";
      > var $username = "root";
      > var $password = "";
      >
      > var $connection;
      >
      > function dbObject ($server, $database, $username, $password) {
      > if ($this->connection = mysql_connect($ server, $username,
      > $password)) {
      > mysql_select_db ($database, $this->connection);
      > }
      > }
      >
      > function query($sql) {
      > $dbO = mysql_query($sq l, $this->connection);
      > if (strtolower(sub str($sql, 0, 6))=="select") {
      > $ret = Array();
      > if (!$dbO) {print "SQL: {$sql} <BR /> FAILED";}
      > while ($dbR = mysql_fetch_arr ay($dbO)) {$ret[] = $dbR;}
      > }else{
      > $ret = $dbO;
      > }
      > return $ret;
      > }
      >
      > function close() {
      > mysql_close($th is->connection);
      > }
      > }
      >
      > ?>[/color]

      Huge thanks. I'm going to try it out right now =)
      Thanks again.


      Comment

      • Christopher Finke

        #4
        Re: Optimizing a function...

        "StinkFinge r" <stinky@pinky.c om> wrote in message
        news:10fh4goqjc nsmcf@corp.supe rnews.com...[color=blue]
        > Does anyone see any optimizations that I can make
        >
        > function table_array ($sql_string)
        > {
        > $result = mysql_query($sq l_string) or die("dead");
        > $num_rows = mysql_num_rows( $result);
        > $num_fields = mysql_num_field s($result);
        > $j = 0;
        > $x = 1;
        > while ($row = mysql_fetch_arr ay($result)) {
        > for($j = 0;$j < $num_fields;$j+ +) {
        > $name = mysql_field_nam e($result, $j);
        > $arr[$x][$name] = $row[$name];
        > }
        > $x++;
        > }
        > return array('arr' => $arr, 'rows' => $num_rows, 'fields' =>[/color]
        $num_fields);[color=blue]
        > }[/color]

        You could do the following:

        function table_array ($sql_string){
        $result = mysql_query($sq l_string) or die("dead");

        while ($row = mysql_fetch_arr ay($result, MYSQL_ASSOC)) {
        $arr[] = $row;
        }

        return array('arr' => $arr, 'rows' => mysql_num_rows( $result), 'fields'
        => mysql_num_field s($result));
        }

        This should be identical, but with less variable declarations and
        assignments.

        Chris Finke


        Comment

        • StinkFinger

          #5
          Re: Optimizing a function...

          "Christophe r Finke" <chris@efinke.c om> wrote in message
          news:2lrk71Ffn6 62U1@uni-berlin.de...[color=blue]
          > "StinkFinge r" <stinky@pinky.c om> wrote in message
          > news:10fh4goqjc nsmcf@corp.supe rnews.com...[color=green]
          >> Does anyone see any optimizations that I can make
          >>
          >> function table_array ($sql_string)
          >> {
          >> $result = mysql_query($sq l_string) or die("dead");
          >> $num_rows = mysql_num_rows( $result);
          >> $num_fields = mysql_num_field s($result);
          >> $j = 0;
          >> $x = 1;
          >> while ($row = mysql_fetch_arr ay($result)) {
          >> for($j = 0;$j < $num_fields;$j+ +) {
          >> $name = mysql_field_nam e($result, $j);
          >> $arr[$x][$name] = $row[$name];
          >> }
          >> $x++;
          >> }
          >> return array('arr' => $arr, 'rows' => $num_rows, 'fields' =>[/color]
          > $num_fields);[color=green]
          >> }[/color]
          >
          > You could do the following:
          >
          > function table_array ($sql_string){
          > $result = mysql_query($sq l_string) or die("dead");
          >
          > while ($row = mysql_fetch_arr ay($result, MYSQL_ASSOC)) {
          > $arr[] = $row;
          > }
          >
          > return array('arr' => $arr, 'rows' => mysql_num_rows( $result), 'fields'
          > => mysql_num_field s($result));
          > }
          >
          > This should be identical, but with less variable declarations and
          > assignments.
          >[/color]

          Thank you also - this is great. I can use this until I fully understand the
          classes, etc.


          Comment

          • Tony Marston

            #6
            Re: Optimizing a function...

            If you want to convert a result set into an associative array then try this
            for size:

            // convert result set into a simple associative array for each row
            while ($row = mysql_fetch_ass oc($result)) {
            $array[] = $row;
            } // while

            return $array;

            As you can see this takes the associative array produced by
            mysql_fetch_ass oc() and appens it to $array. It does not have to waste time
            in iterating over every field in each row.

            I know this code works as I have been using it for several years. Take a
            look at http://www.tonymarston.co.uk/php-mys...seobjects.html for
            details. This gives a basic introduction into classes, and how to create a
            class to handle all your database access.

            HTH.

            --
            Tony Marston

            This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




            "StinkFinge r" <stinky@pinky.c om> wrote in message
            news:10fh4goqjc nsmcf@corp.supe rnews.com...[color=blue]
            > All,
            > This is a function that I use quite often. I pass in my SQL string and
            > return the results as an array. Does anyone see any optimizations that I[/color]
            can[color=blue]
            > make (eventually I would like to make a class, but I am still new).
            >
            > function table_array ($sql_string)
            > {
            > $result = mysql_query($sq l_string) or die("dead");
            > $num_rows = mysql_num_rows( $result);
            > $num_fields = mysql_num_field s($result);
            > $j = 0;
            > $x = 1;
            > while ($row = mysql_fetch_arr ay($result)) {
            > for($j = 0;$j < $num_fields;$j+ +) {
            > $name = mysql_field_nam e($result, $j);
            > $arr[$x][$name] = $row[$name];
            > }
            > $x++;
            > }
            > return array('arr' => $arr, 'rows' => $num_rows, 'fields' =>[/color]
            $num_fields);[color=blue]
            > }
            >
            > Always trying to shave off some access times =)
            > Thanks.
            >
            >[/color]


            Comment

            Working...