while loop question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: while loop question

Post by davex »

Hi,

Yes simple - my schoolboy error :oops:

Glad all is working now. Will take you up on that dinner if I'm ever your way. Likewise if you ever find yourself on the Namibia/Angola border with some time to kill give me a shout :P

Regards,

Dave.
fbachofner
Forum Newbie
Posts: 18
Joined: Sun Feb 28, 2010 6:29 am

Re: while loop question

Post by fbachofner »

Hi Miko:
mikosiko wrote:in your code you were doing an initial call to mysql_fetch_record, therefore your while loop will not start in the first record but in the second.
I actually removed that first call as soon as it was explained it wasn't necessary until later in the code.

mikosiko wrote:What I do suggest you is to take a look to the code that Dave gave to you several posts ago using an array (as I did suggest in my first post)... that is the simples way to solve (no the only one)... your solution is there... anything else is going in circles... why you don't take that code.. implement it and ask anything that you do not understand there?
Sorry I was not clear on my progress in my earlier post(s). Thanks to you and Dave I actually have had this running correctly [the solution using array() ] since Sunday evening. WooHoo!!

My last questions about reordering statements were because I wanted to figure out why the while construct using $count and $row were not working.

You'll note in my post to Dave above I figured that out TOO. But I'm using array() as you originally suggested -- it's definitely more powerful.

mikosiko wrote:will be easy... :)
It actually was, once I started thinking "scripting" and just put a lot of time on task! ;-)


There's still ONE concept that bothers me though:

I thought a construct like

Code: Select all

$ExhibitsQuery_result = mysql_query($ExhibitsQuery)
was what "fetched" the data and that that fetch_assoc put it ALL into an array.

It appears (to ME) that these statements are one step removed from their respective results:
  • mysql_query seems to PREP the query but not actually run it
  • fetch_assoc seems to run the query . . . but only a line at a time (one has to iterate through) -- I wouldn't call that an array (as the PHP docs describe), it's just ONE row!
Perhaps someone can better explain this to me. I fear I might be misunderstanding something that could become important to me in future coding . . .

Thanks again!

Felix
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: while loop question

Post by mikosiko »

fbachofner wrote:....There's still ONE concept that bothers me though:

I thought a construct like

Code: Select all

$ExhibitsQuery_result = mysql_query($ExhibitsQuery)
was what "fetched" the data and that that fetch_assoc put it ALL into an array.

It appears (to ME) that these statements are one step removed from their respective results:
  • mysql_query seems to PREP the query but not actually run it
  • fetch_assoc seems to run the query . . . but only a line at a time (one has to iterate through) -- I wouldn't call that an array (as the PHP docs describe), it's just ONE row!
Thanks again!

Felix

Let me try to explain it in a simple way:

mysql_query efectively run the query and get the records associated to your select (or could be other clause too), as a result of that execution your select can return 0,1, or 1 millions results, therefore those results need to be stored in some place... that place is a RECORDSET... in your code the recordset is $ExhibitsQuery_result.


So.. now you have to have some mechanism to work over the records stored in the recordset.... in your case the mysql_fetch_assoc (could be other too) and some kind of loop to process all the records.....

hope this clarify the concept for you

Miko
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: while loop question

Post by davex »

Hi,

mysql_query will actually run the query - MySQL will then store a results set (although please note it is entirely possible that clever old MySQL actually does something other than just storing the entire results and does indeed re-query but for the purposes of what you can "see" from the PHP interface it is the case) this is returned in PHP as a resource (a link to some external data http://www.php.net/manual/en/language.t ... source.php).

Calling mysql_fetch_assoc (or mysql_fetch_array etc) will return a row and move the data pointer onto the next one.

When your connection is closed (usually just when the script finishes if not using persistent connections) then your results set will be lost/cleared.

I like to (though you hardly ever see it) call mysql_free_result($query_result) just to make sure it gets cleared, especially if I know my script might be using persistent connects and/or not explicitly closing the data connection.

Hope that makes sense.

Regards,

Dave.
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: while loop question

Post by davex »

Hi,

Oh on reading the docs it seems that it is PHP doing the buffering with mysql_query. There is also a mysql_unbuffered_query which you can start reading data from straight away as the query is being executed by MySQL.

You would still read it into your script a row at a time though.

Cheers,

Dave.
Post Reply