Re: **_fetch_row?
| From: | Daniel Convissor | Date: | Thu, 12 Oct 2000 16:50:17 +0000 |
| Subject: | Re: **_fetch_row? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-19846@lists.php.net to get a copy of this message | ||
Hey Jari:
> I would like if someone could explain in a "beginners" language how to use
> the fetch_row commands? I do not really understand that one... I have
> noticed that it can be used with almost all RDBMS....
fetch_row is used to create an enumerated array of the present record (aka
row) from a query result.
An array is a variable which contains values in key/value pairs:
$Array[0] = "something";
$Array[1] = "another thing";
That's an "enumerated" array because the keys are numbers. Arrays can also
be "associative," using words as the index:
$Array["blah"] = "something";
$Array["foo"] = "another thing";
So, getting back to fetch_row, it grabs the data out of the present query
result row and sticks it into an enumerated array. The index keys are
numbered, where 0 contains the data in the first field (aka column), 1
contains the data in the second field, etc...
Thus, as an example, let's run a query, then put the results through
fetch_row:
# assuming you already connected and selected database...
$Result = mysql_query("SELECT Name, Address, Phone FROM Contacts");
$Array = mysql_fetch_row($Result);
Now, $Array[0] will contain the data from the Name field, $Array[1] will have
the Address info, while $Array[2] has their Phone number.
Often, you'll want to loop through the entire query result, displaying the
data from each row (record). Let's show everyone's name:
$Result = mysql_query("SELECT Name, Address, Phone FROM Contacts");
while ( $Array = mysql_fetch_row($Result) ) {
echo "<br />Name: $Array[0]\n";
}
The mysql_fetch_array() function works similarly, though it can return both
enumerated and/or associative arrays.
Enjoy,
--Dan
--
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
More than just answers. Solutions. (SM)
http://www.analysisandsolutions.com/
4015 7 Av #4, Brooklyn NY 11232 v: 718-854-0335 f: 718-854-0409