when is a database resource pointer not valid, and what test can be run?

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

    when is a database resource pointer not valid, and what test can be run?

    I posted before, but have now narrowed my problem down to this method.
    At the start of the method, I test to make sure that I have a
    resource, a pointer to data returned from a database. This test is
    coming back true, so the next line runs, which attempts to get the
    next row from the dataset. This brings back nothing. On the queries
    I'm running right now, the first row will be fetched, but then no
    further rows. If I expect 20 rows back, I get one, then 19 errors. I
    use phpMyAdmin to run the query in another environment, to be sure of
    what I should be expecting.

    Any ideas why the resource pointer might still test valid, yet return
    nothing?




    /**
    * 11-04-03 - it is important that the resource which points to the
    returned dataset gets passed into this method
    * by reference, not by copy, or else, in the outside code that is
    calling this method, the pointer in that resource
    * will never advance to the next resource row.
    */
    function dsRowIntoArrayW ithStringIndex( &$dsResult) {
    // 11-04-03 - this first lines test to see if anything came back
    from the datastore
    if (is_resource($d sResult)) {
    $row = mysql_fetch_arr ay($dsResult, MYSQL_ASSOC);
    $row = $this->stripslashesFr omEntryWithKeyI ndex($row);
    return $row;
    } else {
    $this->resultsObjec t->addToErrorResu lts("In
    dsRowIntoArrayW ithStringIndex( ), in the class McFormatResults MySql, we
    expected the method to be handed a pointer to a database return
    resource, but we were not.");
    }
    }
  • Pedro Graca

    #2
    Re: when is a database resource pointer not valid, and what test can be run?

    lawrence wrote:[color=blue]
    > Any ideas why the resource pointer might still test valid, yet return
    > nothing?[/color]

    The variable still holds a resource (that's what you are testing).
    But the resource may be no longer valid (the database connection may
    have been closed by the server).

    On the manual I found the mysql_ping() function.
    Ping a server connection or reconnect if there is no connection


    Never used it, but it might be what you need (PHP >= 4.3.0)
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • lawrence

      #3
      Re: when is a database resource pointer not valid, and what test can be run?

      Pedro Graca <hexkid@hotpop. com> wrote in message news:<c2antr$1r ap4q$2@ID-203069.news.uni-berlin.de>...[color=blue]
      > lawrence wrote:[color=green]
      > > Any ideas why the resource pointer might still test valid, yet return
      > > nothing?[/color]
      >
      > The variable still holds a resource (that's what you are testing).
      > But the resource may be no longer valid (the database connection may
      > have been closed by the server).
      >
      > On the manual I found the mysql_ping() function.
      > http://www.php.net/mysql_ping
      >
      > Never used it, but it might be what you need (PHP >= 4.3.0)[/color]

      Well, if it is available since 4.3 then I can't use it, since part of
      my projects goal is to stay backwards compatible to 4.0.6. I guess I
      can test php versions and run ping if the version is right, I do that
      in some special cases, but for the most part I hate doing that.

      Also, it is a bit of a band-aid that doesn't solve the real problem.
      The real problem is why the connection goes dead? Since this code was
      working a week ago, and now isn't, I need to look for something I've
      done in the last week that has caused the resource pointer to become
      invalid between loops. Any suggestions about what that might be?
      Clearly, I'm not over writing the variable, since it still tests true
      as a resource. I have a for() loop in my code, and the code works
      correctly during the first loop, but then I get nothing back on the
      rest of the iterations through the loop.

      What kind of things might cause the database return to be lost?

      Comment

      • thumb_42@yahoo.com

        #4
        Re: when is a database resource pointer not valid, and what test can be run?


        I'm not sure how this would work in PHP, but in other languages I've
        implemented a 'ping' from a simple "SELECT 1" type of query.

        Wish PHP had genuine, real exception handling, that would solve a lot
        of database problems. (I don't mean an error function handler ;-/ )

        Jamie

        lawrence <lkrubner@geoci ties.com> wrote:[color=blue]
        > Pedro Graca <hexkid@hotpop. com> wrote in message news:<c2antr$1r ap4q$2@ID-203069.news.uni-berlin.de>...[color=green]
        >> lawrence wrote:[color=darkred]
        >> > Any ideas why the resource pointer might still test valid, yet return
        >> > nothing?[/color]
        >>
        >> The variable still holds a resource (that's what you are testing).
        >> But the resource may be no longer valid (the database connection may
        >> have been closed by the server).
        >>
        >> On the manual I found the mysql_ping() function.
        >> http://www.php.net/mysql_ping
        >>
        >> Never used it, but it might be what you need (PHP >= 4.3.0)[/color]
        >
        > Well, if it is available since 4.3 then I can't use it, since part of
        > my projects goal is to stay backwards compatible to 4.0.6. I guess I
        > can test php versions and run ping if the version is right, I do that
        > in some special cases, but for the most part I hate doing that.
        >
        > Also, it is a bit of a band-aid that doesn't solve the real problem.
        > The real problem is why the connection goes dead? Since this code was
        > working a week ago, and now isn't, I need to look for something I've
        > done in the last week that has caused the resource pointer to become
        > invalid between loops. Any suggestions about what that might be?
        > Clearly, I'm not over writing the variable, since it still tests true
        > as a resource. I have a for() loop in my code, and the code works
        > correctly during the first loop, but then I get nothing back on the
        > rest of the iterations through the loop.
        >
        > What kind of things might cause the database return to be lost?[/color]

        Comment

        • lawrence

          #5
          Re: when is a database resource pointer not valid, and what test can be run?

          thumb_42@yahoo. com wrote in message news:<p2k2c.494 072$na.1169935@ attbi_s04>...[color=blue]
          > I'm not sure how this would work in PHP, but in other languages I've
          > implemented a 'ping' from a simple "SELECT 1" type of query.
          >
          > Wish PHP had genuine, real exception handling, that would solve a lot
          > of database problems. (I don't mean an error function handler ;-/ )
          >
          > Jamie[/color]

          I'm a little confused. If I run "SELECT 1" as a ping, then suddenly
          I've got a pointer to a new return, yes? It doesn't bring the old
          pointer back to life, yes? Or did you mean that this is a good way to
          test and make sure the database connection is still there?

          I wish I had more control over things. Where are the database returns
          held, in MySql or in PHP? If in MySql, can I send a message to MySql
          and say, "Keep holding this, I'm not done yet?" Is there a list of ids
          of all recent database returns?

          Maybe its moot, I've decided to redo the whole damn thing. I've wasted
          3 days on this and I'm not willing to waste a 4th. But it would have
          been nice to figure out what was really going on.









          [color=blue]
          > lawrence <lkrubner@geoci ties.com> wrote:[color=green]
          > > Pedro Graca <hexkid@hotpop. com> wrote in message news:<c2antr$1r ap4q$2@ID-203069.news.uni-berlin.de>...[color=darkred]
          > >> lawrence wrote:
          > >> > Any ideas why the resource pointer might still test valid, yet return
          > >> > nothing?
          > >>
          > >> The variable still holds a resource (that's what you are testing).
          > >> But the resource may be no longer valid (the database connection may
          > >> have been closed by the server).
          > >>
          > >> On the manual I found the mysql_ping() function.
          > >> http://www.php.net/mysql_ping
          > >>
          > >> Never used it, but it might be what you need (PHP >= 4.3.0)[/color]
          > >
          > > Well, if it is available since 4.3 then I can't use it, since part of
          > > my projects goal is to stay backwards compatible to 4.0.6. I guess I
          > > can test php versions and run ping if the version is right, I do that
          > > in some special cases, but for the most part I hate doing that.
          > >
          > > Also, it is a bit of a band-aid that doesn't solve the real problem.
          > > The real problem is why the connection goes dead? Since this code was
          > > working a week ago, and now isn't, I need to look for something I've
          > > done in the last week that has caused the resource pointer to become
          > > invalid between loops. Any suggestions about what that might be?
          > > Clearly, I'm not over writing the variable, since it still tests true
          > > as a resource. I have a for() loop in my code, and the code works
          > > correctly during the first loop, but then I get nothing back on the
          > > rest of the iterations through the loop.
          > >
          > > What kind of things might cause the database return to be lost?[/color][/color]

          Comment

          • lawrence

            #6
            Re: when is a database resource pointer not valid, and what test can be run?

            This is absolutely killing me. A whole week has gone by and I still
            can't fix this. And it was working before, so I just can figure out
            what is wrong. I've rewritten the whole database connect system so
            there is less masking and less wrapping and everything is more direct,
            but I still face the same problem - I run a call against a database, I
            get back a result, I go into a loop to print out the results, the
            first row works comes back, and I get nothing thereafter.

            I have a select object that has member objects - a query object and
            then a getResults object. The query object works perfectly and runs
            the query and comes back with a valid database pointer. The pointer is
            then given to the getResults object. The getResults object is supposed
            to return rows. It works the first time, and not thereafter. The
            pointer to the database return is stored as a class variable in the
            getResults object. Again, this works go get the first row. But somehow
            after the first row the pointer goes dead.

            Below is the class, if anyone can see why the pointer goes dead, I'd
            be very grateful. Again, getRowAsArrayWi thStringIndex() works for the
            first row but not thereafter. Why? getCountOfRetur n gets an accurate
            count.




            class McDatastoreResu ltsMySql {


            var $datastoreResul tsPointer = null;
            var $resultsObject = null;




            function McDatastoreResu ltsMySql() {
            $controllerForA ll = & getController() ;
            $this->resultsObjec t = & $controllerForA ll->getObject("McR esults", "
            in the constructor of the class McDatastoreResu ltsMySql.");
            }





            function setInfoResource Pointer($result s) {
            if (is_resource($r esults)) {
            $className = get_class($this->formatObject );
            $this->resultsObjec t->debugNotes(" In setInfoResource Pointer(), in
            the class McDatastoreResu ltsMySql, we've been given a pointer to a
            database resource and now assign to our class variable.",
            "McDatastoreRes ults");
            $this->datastoreResul tsPointer = $results;
            } else {
            $this->resultsObjec t->addToErrorResu lts("In
            setInfoResource Pointer(), in the class McDatastoreResu ltsMySql, we
            expected to be given a pointer to a database resource, but we were
            not.", "McDatastoreRes ultsMySql");
            }
            }





            /**
            * 12-24-03 - getter
            *
            * We cannot call getRowAsArrayWi thStringIndex() in a loop unless we
            know how many items there are in the return.
            * Actually, there are other ways, but this is one.
            *
            * $this->datastoreResul tsPointer - this is a resource pointer,
            pointing to the data returned from the last SELECT call
            * to the datastore. Belongs to the parent class,
            McDatastoreResu ltsFormat
            *
            * returns integer
            */
            function getCountOfRetur n() {
            if (is_resource($t his->datastoreResul tsPointer)) {
            $numberOfRowsRe turned =
            mysql_num_rows( $this->datastoreResul tsPointer);
            return $numberOfRowsRe turned;
            } else {
            $this->resultsObjec t->addToErrorResu lts("In the getCountOfRetur n(),
            in the class McDatastoreResu ltsMySql , we expected to be given a
            pointer to data returned from the database during the last SELECT
            call. Instead we got: $this->datastoreResul tsPointer ");
            }
            }





            /**
            * 11-04-03 - it is important that the resource which points to the
            returned dataset gets passed into this method
            * by reference, not by copy, or else, in the outside code that is
            calling this method, the pointer in that resource
            * will never advance to the next resource row.
            */
            function getRowAsArrayWi thStringIndex() {
            // 11-04-03 - this first lines test to see if anything came back
            from the datastore
            if (is_resource($t his->datastoreResul tsPointer)) {
            $row = mysql_fetch_ass oc($this->datastoreResul tsPointer);
            $row = $this->stripslashesFr omEntryWithKeyI ndex($row);
            return $row;
            } else {
            $this->resultsObjec t->addToErrorResu lts("In
            getRowAsArrayWi thStringIndex() , in the class McDatastoreResu ltsMySql,
            we expected the method to be handed a pointer to a database return
            resource, but we were not.");
            }
            }



            /**
            * 03-08-04 - manipulator
            *
            * A private method for getting the back slashes out of data returned
            from a database.
            *
            * @param - $entry - array - this method expects to be handed a one
            dimensional array, one row of a database return,
            * which we then loop through and run stripslashes on the values of
            every field, then we return the processed array
            *
            * private
            * returns array - one dimensional
            */
            function stripslashesFro mEntryWithKeyIn dex($entry) {
            if (is_array($entr y)) {
            while (list($key, $val) = each($entry)) {
            $entry[$key] = stripslashes($v al);
            }
            } else {
            $this->resultsObjec t->addToErrorResu lts("In
            stripslashesFro mEntryWithKeyIn dex(), in the class
            McDatastoreResu ltsMySql, we expected the method to be handed an array,
            but it was not.");
            }
            return $entry;
            }







            }


            ?>

            Comment

            Working...