newbie question about fetch_array

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

Post Reply
svenxie
Forum Newbie
Posts: 2
Joined: Sat Sep 17, 2005 12:49 pm

newbie question about fetch_array

Post by svenxie »

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Code: Select all

$myc = mysql_connect("localhost", "root", "");
mysql_select_db("mydatenbank", $myc);

$query1 = mysql_query("SELECT A.LRUsername, A.LRPassword
FROM community A
WHERE A.LRUsername NOT IN
(SELECT user FROM arsc_registered_users)
", $myc);

while ($result = mysql_fetch_array($query1))
{

echo $result["LRUsername"]." <br />\n ";
echo $result["LRPassword"]." <br />\n ";
}

how mysql_fetch_array works?
why does the pointer move by itself without FOR NEXT or sth like $i++?

and, is $result just for one row? and wait till the next turn of WHILE
when the pointer moves to next, it reads another row of data?


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: newbie question about fetch_array

Post by feyd »

svenxie wrote:how mysql_fetch_array works?
mysql fetch array returns, unless specified, will return an array of both numeric and named indices of the current row (if available)
svenxie wrote:why does the pointer move by itself without FOR NEXT or sth like $i++?
the result resource keeps a pointer to which row it is currently on of the result set. This internal row pointer is advanced on each call to a fetch function.
svenxie wrote:and, is $result just for one row? and wait till the next turn of WHILE
when the pointer moves to next, it reads another row of data?
In your instance of code, $result is one row on each call to mysql_fetch_array() until there are no more rows to fetch, in which case it will be false.
svenxie
Forum Newbie
Posts: 2
Joined: Sat Sep 17, 2005 12:49 pm

Post by svenxie »

thank you very much.

sorry that i'm new here and not familiar to the rules,
also new to php.

thanks again.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I would say use mysql_fetch_assoc, being new, unless you specifically need indiced arrays.

that way, when debugging, you'll just see an array of associative values, like:

Code: Select all

Array (
   [field1] => fieldonevalue,
   [field2] => field2value,
   [etc] => more values
   )
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply