mysql_data_see for for() loops?

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

    mysql_data_see for for() loops?

    Am I right to say mysql_data_seek allows me to walk through a database
    return as I would an array?


    for ($1=0; $i < $num; $i++) {
    mysql_data_see( $result, $i);
    $row = mysql_fetch_arr ay($results);
    }

    I still haven't solved the problem I mentioned in another post (I can
    only get the first row back from my returns), so I'm tempted to go
    this route.
  • Paul Wellner Bou

    #2
    Re: mysql_data_see for for() loops?

    lawrence wrote:[color=blue]
    > Am I right to say mysql_data_seek allows me to walk through a database
    > return as I would an array?[/color]
    Why???
    [color=blue]
    > for ($1=0; $i < $num; $i++) {
    > mysql_data_see( $result, $i);
    > $row = mysql_fetch_arr ay($results);
    > }[/color]

    I suppose you're looking for something like this?

    while($row = mysql_fetch_ass oc($res))
    {
    // your code which does anything with $row
    }

    Just rtfm a little bit...
    --> http://php.net/mysql_fetch_array
    (Three examples doing this.)

    Comment

    • Mike Peters

      #3
      Re: mysql_data_see for for() loops?

      On 2004-03-05, lawrence wrote:[color=blue]
      > Am I right to say mysql_data_seek allows me to walk through a database
      > return as I would an array?
      >
      >
      > for ($1=0; $i < $num; $i++) {
      > mysql_data_see( $result, $i);
      > $row = mysql_fetch_arr ay($results);
      > }
      >
      > I still haven't solved the problem I mentioned in another post (I can
      > only get the first row back from my returns), so I'm tempted to go
      > this route.[/color]

      mysql_data_seek lets you move the array pointer to the specified point.
      Why not just use:

      while ( $row = mysql_fetch_arr ay($results)) {
      // Do your stuff
      }

      ?

      --
      Mike Peters
      mike [-AT-] ice2o [-DOT-] com
      I am a DevOps and Cloud architecture consultant based in Northumberland in the UK. I provide consultancy on DevOps and private and public cloud solutions. I design and implement continous integration and continuous delivery solutions using Open Source, bespoke and commercial off the shelf software. I can also provide training and tuition for you or your team in DevOps best practices, tooling and solutions, OpenSource software, automation and cloud architecture solutions.

      Comment

      • lawrence

        #4
        Re: mysql_data_see for for() loops?

        Mike Peters <o0__mike__0oRE MOVE@THIShotmai l.com> wrote in message news:<1daf04fb5 09c2d5e77cbcf1c bbc283ea@news.t eranews.com>...[color=blue]
        > mysql_data_seek lets you move the array pointer to the specified point.
        > Why not just use:
        >
        > while ( $row = mysql_fetch_arr ay($results)) {
        > // Do your stuff
        > }[/color]

        Because that would mean using msyql specific commands in the main part
        of my code. I've masked out all the mysql stuff. I've a selectObject
        that all queries get run through. It might be getting data from mysql
        or postgre or a file or an xml stream. The code doesn't know where the
        data comes from. So my loop looks like this:


        $selectObject->setQuery("GetA llWeblogEntries ");
        $selectObject->getData();
        $howMany = $selectObject->getCount();

        for ($i=0; $i < $howMany; $i) {
        $row = $selectObject->getNextRow() ;
        }



        This was working perfectly till this week, when I made some changes
        and busted it. I'm not sure what I did wrong. The getCount method
        still works correctly and returns the correct number of items that
        have been returned. But then in the for() loop, the getNextRow()
        method only works the first time. After that it returns nothing. I'm
        very confused by this - I'd think either the pointer would advance to
        the next row, or it would stay stuck on the first row and return that
        for how ever many times $howMany equals. But instead it works once and
        then returns nothing. However, it remains a valid resource pointing to
        a database return. I know this because inside of getNextRow the result
        pointer gets tested every time with is_resource($re sults) and it never
        tests false. So I'm confused about what is going on.

        Today, in desperate attempt to get the code working again, I changed
        things so that I could pass the loop index into getNextRow and feed it
        to mysql_data_seek . But it still didn't work. This is what I'm
        currently trying:


        $selectObject->setQuery("GetA llWeblogEntries ");
        $selectObject->getData();
        $howMany = $selectObject->getCount();

        for ($i=0; $i < $howMany; $i) {
        $row = $selectObject->getNextRow($i) ;
        }




        then, inside getNextRow(), I go:

        if (is_resource($r esult)) {
        $success = mysql_data_seek ($result, $i);
        if ($success) {
        $resultsObject->debugNotes("Te sted true");
        } else {
        $resultsObject->addToErrorResu lts("Tested false");
        }
        }

        And it keeps testing false. I'm a little stunned by this. Why is
        mysql_data_seek () failing if the resource is testing true in
        is_resource()? I'm having a damned hard time figuring it out.

        I also echo out the actual query to the screen and then run it through
        phpMyAdmin so I can see what kind of return I should expect. All day
        I've been running my tests on one query where I know I should be
        getting back 3 entries. phpMyAdmin tells me so and the method
        getCount() correctly comes back with 3. And, as I said, the first row
        comes back fine. After that nothing works.

        I'm passing the resource pointer by reference, so what happens inside
        the object should effect what happens outside.

        Comment

        • Pedro Graca

          #5
          Re: mysql_data_see for for() loops?

          lawrence wrote:[color=blue]
          > This was working perfectly till this week, when I made some changes
          > and busted it. I'm not sure what I did wrong.[/color]

          Since I discovered CVS I wonder how I was able to do anything at all
          without using it :-)

          There's a more recent thing called "SVN" (if I remember correctly)
          which I haven't yet tried out.
          [color=blue]
          > [...]
          > then, inside getNextRow(), I go:
          >
          > if (is_resource($r esult)) {
          > $success = mysql_data_seek ($result, $i);
          > if ($success) {
          > $resultsObject->debugNotes("Te sted true");
          > } else {
          > $resultsObject->addToErrorResu lts("Tested false");
          > }
          > }
          >
          > And it keeps testing false. I'm a little stunned by this. Why is
          > mysql_data_seek () failing if the resource is testing true in
          > is_resource()? I'm having a damned hard time figuring it out.[/color]

          Test the return value from all mysql_* functions with mysql_error().

          $success = mysql_data_seek ($result, $i);
          if ($success) {
          $resultsObject->debugNotes("Te sted true");
          } else {
          $resultsObject->addToErrorResu lts("Tested false with " . mysql_error());
          }
          [color=blue]
          > I also echo out the actual query to the screen and then run it through
          > phpMyAdmin so I can see what kind of return I should expect. All day
          > I've been running my tests on one query where I know I should be
          > getting back 3 entries. phpMyAdmin tells me so and the method
          > getCount() correctly comes back with 3. And, as I said, the first row
          > comes back fine. After that nothing works.[/color]

          Do you have error_reporting set to show all errors, warnings and
          notices?


          I have php.ini configured to *not* show any errors and save them to a
          error log. However, when I'm having trouble with a script I do a

          define('DEBUG', '1');

          before requiring my standard include file, which has

          if (DEBUG) {
          error_reporting (E_ALL);
          ini_set('displa y_errors', '1');
          ini_set('error_ log', '');
          }

          I find it much easier to see the errors in the browser than having to
          open the error log.
          --
          --= my mail box only accepts =--
          --= Content-Type: text/plain =--
          --= Size below 10001 bytes =--

          Comment

          Working...