Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help understanding mysql_fetch_array

Status
Not open for further replies.

y2k1981

Programmer
Joined
Aug 2, 2002
Messages
773
Location
IE
the search feature is unavailable so hopefully somebody can help me.

I can't always seem to get this to work, here's what I have:
Code:
$sqlsearch = mysql_query("SELECT * FROM table_name");
$result = mysql_fetch_array($result);

Let's assume there are 3 rows in the table_name table. What should be returned to $result? What I want to do is store the results in an array, but that doesn't seem to work. Sometimes I just get 1 result, sometimes I get an error. Can somebody explain to me how I would go about feeding the results from the above query into an array, and maybe explain to me what exactly mysql_fetch_array does? How is it different from mysql_fetch_row? I've been trying to read up on it, but because I've been dipping in and out of different parts of the book, it doesn't all make sense to me.

thanks in advance
 
if you have a table:
create table temp (
foo int,
bar int
);

You can now use
Code:
$result = mysql_query("SELECT * FROM table_name");
$array = mysql_fetch_array($result, NULL, MYSQL_BOTH);

echo $array['foo'] . " " . $array['bar'];
you can also use it in a loop... When you use NULL as linenumber it automaticly fetch the row and step to next row...
Code:
$result = mysql_query("SELECT * FROM table_name");

while ( $array = mysql_fetch_array($result, NULL, MYSQL_BOTH) ) {
echo $array['foo'] . " " . $array['bar'];
}
 
thanks, would you mind explaining to me what the NULL and MYSQL_BOTH are doing?
 
Please read faq434-3850 step 4 to learn what you want to know.

As to your second question:
According to the PHP manual mysql_fetch_array() only takes two parameters. The first parameter is the resource identifier returned by mysql_query() and the second param allows for different types of array to be returned:
MYSQL_ASSOC -> associative array with column name as key and content as value (Same as mysql_fetch_assoc())
MYSQL_NUM -> numerically keyed array (Same as mysql_fetch_row())
empty or MYSQL_BOTH -> both types, i.e. numerically and associative arrays (the only legit use of this)
 
I think kama1 was meaning mysql_fetch_row() rather than mysql_fetch_array()?

_________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
OK, I think I'm getting there, but I thought that mysql_fetch_array and mysql_fetch_row were the same just that for mysql_fetch_row you had to refer to the column by it's location, eg the first column would be $rowResult[0] etc whereas with mysql_fetch_array you can refer to it by name?
 
mysql_fetch_array() returns both numeric and associative keys when involed without a second parameter.
mysql_fetch_row() return numeric keys only.

Both are "the same" as afar as they each retrieve one row from the resource.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top