Jump to content

Help understanding fetch_assoc/array


nloding

Recommended Posts

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 / PAGES
Carrie / King, Stephen / 200
Hobbit, The / Tokein, JRR / 198
Scorpions, The / Meyers, Walter Dean / 176

So 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!
Link to comment
Share on other sites

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 rowcount

while($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 them

echo '<p>title: '.$row['title'].'<br />'; //display title
echo 'author: '.$row['author'].'<br />'; //display author
echo 'pages: '.$row['pages'].'<br />'; //display pages
$rowcount++; //increment the $rowcount variable
} // end our while loop

?>
[/code]
Link to comment
Share on other sites

[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, TheScorpions

I want to know what fetch_array() is doing in the background.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.