nloding Posted January 24, 2007 Share Posted January 24, 2007 I'm relatively new to PHP and MySQL, and I'm a little confused as to how the fetch_assoc() and fetch_array() objects work. I understand that they return arrays, but how does that work if there, say, 10 rows in the table. For example, I have a table with the fields "title", "author", and "pages"; ten rows to the table, so ...TITLE / AUTHOR / PAGESCarrie / King, Stephen / 200Hobbit, The / Tokein, JRR / 198Scorpions, The / Meyers, Walter Dean / 176So the I run the following PHP/MySQL sequence:[code]$query = 'select * from books';$result = mysqli->query($query);while($row = $result->fetch_array();) {echo '<p>title: '.$row['title'].'<br />';echo 'author: '.$row['author'].'<br />';echo 'pages: '.$row['pages'].'<br />';}[/code]There are multiple values that could be $row['title'] (Carrie, Hobbit, etc.). So why does that while loop output each value? What is going on behind the scenes? Or is that code above messed up?It's not enough for me to just know how to do it ... I want to know WHY it does it! Quote Link to comment Share on other sites More sharing options...
trq Posted January 25, 2007 Share Posted January 25, 2007 [quote]So why does that while loop output each value?[/quote]Because your query fetches every row. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 25, 2007 Share Posted January 25, 2007 Your question is pretty vague. What is it doing that you think is wrong? Quote Link to comment Share on other sites More sharing options...
chronister Posted January 25, 2007 Share Posted January 25, 2007 Here is a break down of what this is doing. there were a few items I changed as well.[code]<?php$query = 'select * from books'; // set up the "question" for the database$result = mysql_query($query); //set variable named result, and assign mysql_query function$rowcount=0; // set a counter for rowcountwhile($row = $result->fetch_array();) { // start a while loop,set $row var & it assign mysql_fetch _array() echo '<br>Row Number '. $rowcount.'<br><br>'; //echo which row we are displaying//while there are results to display, display themecho '<p>title: '.$row['title'].'<br />'; //display titleecho 'author: '.$row['author'].'<br />'; //display authorecho 'pages: '.$row['pages'].'<br />'; //display pages$rowcount++; //increment the $rowcount variable} // end our while loop?>[/code] Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 25, 2007 Share Posted January 25, 2007 But what is your question? Quote Link to comment Share on other sites More sharing options...
nloding Posted January 25, 2007 Author Share Posted January 25, 2007 [quote author=jesirose link=topic=123916.msg512815#msg512815 date=1169684471]Your question is pretty vague. What is it doing that you think is wrong?[/quote]It's not that I think I'm doing something wrong. It works, or at least is supposed to (I just posted another topic where my code just stopped working). But that's another story.The while loops processes the fetch_array() function, then outputs the values, then runs it again, outputs the new values, etc. How is it moving from one row to another? Otherwise, you'd get something like this:$row['title'] = CarrieHobbit, TheScorpionsI want to know what fetch_array() is doing in the background. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 25, 2007 Share Posted January 25, 2007 Well I can't tell which version it is because it's in a class, but if it's mysql_fetch_array - the manual says:"Returns an array that corresponds to the fetched row and moves the internal data pointer ahead."So that's why it goes to the next one automatically. Quote Link to comment Share on other sites More sharing options...
nloding Posted January 25, 2007 Author Share Posted January 25, 2007 I missed that line somehow! I guess that helps ... it explains why it moves ahead each time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.