hi, i have a query that looks like this:

$sql = 'SELECT *
FROM prices';
$result_prices = $conn -> query($sql) or die(mysqli_error());

i use..

while ($row = $result_prices -> fetch_assoc()) {
...

..to get the rows one after another. but when all rows are displayed i need to go through them one more time. how do i reset the row counter to start from the first row once again??

Thx,
Lubo

    You can't reuse the result. So why not add the addtional processing to the while loop:

    while ($row = $result_prices -> fetch_assoc()) {
        myfirstprocces($row);
        mySecondProcces($row);
    }

    I suppose you mean to write the results at the top or bottom of the page?

    while ($row = $result_prices -> fetch_assoc()) {
      global $firstwrite, $secondwrite;
        myfirstprocces($row);
        mySecondProcces($row);
    }
    echo $firstwrite;
    echo "blah blah blah";
    echo $secondwrite;
    function myfirstprocces($row){
      global $firstwrite;
      $firstwrite.=$row[id];
    }
    function mySecondProcces($row){
      global $secondwrite;
      $secondwrite.=$row[name];
    }
    

      You can use [man]mysqli_data_seek/man if you really want to, but something like what nemonoman suggested (but using pass by reference instead of global variables) would probably be better.

        I fail to understand the hostility often expressed against global varirables. Passing by reference duplicates the effect, and makes later, easily defined access complicated.

        (Signed)
        An Old Codger Who Likes Globals

          I fail to understand the hostility often expressed against global varirables.

          Global variables lead to tight coupling. The function and its caller are tied to the particular global variable. This limits function reuse and makes maintenance more difficult. For example, there is no way to use myfirstprocces() with anything other than $firstwrite, and in the event a maintainer decides that $firstwrite is not accurate enough as a name, he/she has to change it both in the function and in all callers of the function.

          Passing by reference duplicates the effect, and makes later, easily defined access complicated.

          I posit that passing by reference does not make access complicated. It does require one to pass arguments, of course, but this in fact is an advantage since it makes it clear what variables known to the caller may be modified by the function. Global variables, on the other hand, can make it more difficult to reason about the state of a variable at a particular point.

            That kind of thinking eventually leads to object-oriented programming. I like to remember that $_GLOBALS is an object, if you want to think of it that way.

            I very often find that some little hunk of info I grabbed in a function someplace has use somewhere else. A global variable makes a nice temporary van to move the data from place to place.

            In the example we discuss here, I agree that passing by reference might be as good or better than the quickie code I threw together for example purposes. On the other hand, I've seen very complex algorithms written simply to avoid the use of the Dread Global Variable. Globals are a tool, to be used or misued. Sometimes they're the right tool for the job.

            Nemo

              That kind of thinking eventually leads to object-oriented programming. I like to remember that $GLOBALS is an object, if you want to think of it that way.


              The array is actually $GLOBALS, not $
              GLOBALS 😉
              An inconsistency in naming because the $_* style superglobals were introduced later, as I recall.

              I would say that the $_* superglobal arrays act as registries for incoming variables. Consequently, it is clear that functions that access them are not designed to be reusable and have to be modified should they change. Also, since they are registries, they are isolated from normal variables, thus avoiding the variable poisoning problem posed by register_globals.

              I very often find that some little hunk of info I grabbed in a function someplace has use somewhere else. A global variable makes a nice temporary van to move the data from place to place.

              The appropriate mechanisms would be to have that "function someplace" return "some little hunk of info" and "somewhere else" have an appropriate parameter to accept it, or have "function someplace" place "the some little hunk of info" into an argument passed by reference, and then the caller passes the variable aliased to it and "somewhere else". That way the caller and the two functions are insulated from the others' implementation.

              If you are into object based programming, it may well be that the two functions should form part of a class interface, in which case the global variable would become a member variable at object scope ("global" between member functions, if you see it that way). In that case, the private implementation of the class is insulated from the caller.

              Either way, the caller only needs to know the interface (i.e., parameter list and what may be returned) provided by the functions, be they free functions or member functions. It does not have to be concerned with their implementation details, and can be sure that calling them will not unexpectedly change its own state due to an unfortunate choice of variable names. As such, they may freely change their implementation specifics without affecting the caller other than by the interface provided.

              On the other hand, I've seen very complex algorithms written simply to avoid the use of the Dread Global Variable.

              I do not see how the algorithms need to be more complex. The main change is how the variable enters into the current scope. Any examples?

              Globals are a tool, to be used or misued. Sometimes they're the right tool for the job.

              I tend to agree with that, just as I think that gotos are sometimes the right tool for the job. Indeed, the concept of a global registry is something I find useful. However, I strongly disagree with the notion that it is right to use them as a casual substitute for argument passing.

                nemonoman wrote:

                That kind of thinking eventually leads to object-oriented programming.

                Hey! I'm anti-globals and a procedural coder from way back.

                I happen to love the fact that in php, global variables basically aren't. At least not in the classical sense.

                I'm also not the biggest fan of passing by reference, unless you need to update the data structure, you can just pass in a copy as an argument most of the time.

                Not that I never use globals. I do. But they're one step above goto in my book, and should generally be avoided.

                  Do what I do,

                  function blah()
                  {
                  ....query stuff.....
                  while($row=mysql_fetch_assoc($QUERY))
                  {
                  $ReturnThis[]=$row;
                  }

                  if (isset($ReturnThis)) return $ReturnThis;
                  else return false;
                  }

                  $whateva=blah();

                  $whateva is now an array variable you can port, use and reuse all you want.
                  That was the variable is local, easily sessioned , can pass into functions and has all the functionally of arrays !

                    Write a Reply...