How to insert a web link?
Problem with foreach looping...
Hello Guest
  
  • Login
• Register…
• Start blog
  • Who, Where, When
• What can I do?
• What to Read?
  • Polls
• Avatars
• Interests
  • Cities and Countries
• Random blog
• Users search
  • Search
• Games
• Tests
• QAIX
  • Сообщества
• Talxy Chat
• Horoscope
• Online
 
Зарегистрируйся!

QAIX > PHP web-programming > Problem with foreach looping... 8 April 2005 00:31:22

  Recent blog posts: 
  They have birthday today: 
  Forums:   
  Discuss: 
  Recent forum topics: 
  Recent forum comments:
  Moderators:

Problem with foreach looping...

Scott V Nipp 8 April 2005 00:31:22
 I am attempting to use a simple foreach loop on a query result,
and I am only getting the first element of the array back. Here is the
code...

$exclude_query = "SELECT hostname FROM exclusion";
$exclude_results = mysql_query($exclud­e_query, $Prod);
$exclude = mysql_fetch_array($­exclude_results, MYSQL_NUM);

foreach ($exclude as $row) {
echo "$row\n";
}

I don't understand how I am just getting a single item back from
this. The query actually returns all 13 entries in the exclusion table.
I have tested the query via the mysql client. I also get all 13
elements back if I loop via the following.

while ($test = mysql_fetch_array($­results, MYSQL_NUM)) {
echo "$test[0]\n";
}

Is my PHP somehow broke, or am I just missing something here?
Thanks.

Scott Nipp
Phone: (214) 858-1289
E-mail: sn4265@sbc.com
Web: http:\\ldsa.sbcld.s­bc.com

--
PHP Database Mailing List (http://www.php.net­/)
To unsubscribe, visit: http://www.php.net/­unsub.php


Add comment
Mike Johnson 7 April 2005 23:19:17 permanent link ]
 From: NIPP, SCOTT V (SBCSI) [mailto:sn4265@sbc.com]
I am attempting to use a simple foreach loop on a query result,> and I am only getting the first element of the array back. > Here is the> code...>
$exclude_query = "SELECT hostname FROM exclusion";> $exclude_results = mysql_query($exclud­e_query, $Prod);> $exclude = mysql_fetch_array($­exclude_results, MYSQL_NUM);>
foreach ($exclude as $row) {> echo "$row\n";> }>
I don't understand how I am just getting a single item back from> this. The query actually returns all 13 entries in the > exclusion table.> I have tested the query via the mysql client. I also get all 13> elements back if I loop via the following.>
while ($test = mysql_fetch_array($­results, MYSQL_NUM)) {> echo "$test[0]\n";> }>
Is my PHP somehow broke, or am I just missing something here?> Thanks.

mysql_fetch_array()­ only fetches one record of the result set at a time.
Thus, the code's doing what you told it to. The while() loop is what you
want. Why do you need to use a foreach() loop? At that point, there's
nothing to foreach() through. No array has been constructed.

HTH!


--
Mike Johnson Smarter Living, Inc.
Web Developer www.smartertravel.c­om
mike@smarterliving.­com (617) 886-5539

--
PHP Database Mailing List (http://www.php.net­/)
To unsubscribe, visit: http://www.php.net/­unsub.php


Add comment
Wendell Frohwein 8 April 2005 00:31:22 permanent link ]
 Try this

$exclude_query = "SELECT hostname FROM exclusion";
$exclude_results = mysql_query($exclud­e_query, $Prod);

while ($exclude = mysql_fetch_array($­exclude_results)) {
echo $exclude["hostname"]."\n";
}

-Wendell



-----Original Message-----
From: NIPP, SCOTT V (SBCSI) [mailto:sn4265@sbc.com]
Sent: Thursday, April 07, 2005 12:15 PM
To: php-db@lists.php.ne­t
Subject: [PHP-DB] Problem with foreach looping...

I am attempting to use a simple foreach loop on a query result,
and I am only getting the first element of the array back. Here is the
code...

$exclude_query = "SELECT hostname FROM exclusion";
$exclude_results = mysql_query($exclud­e_query, $Prod);
$exclude = mysql_fetch_array($­exclude_results, MYSQL_NUM);

foreach ($exclude as $row) {
echo "$row\n";
}

I don't understand how I am just getting a single item back from
this. The query actually returns all 13 entries in the exclusion table.
I have tested the query via the mysql client. I also get all 13
elements back if I loop via the following.

while ($test = mysql_fetch_array($­results, MYSQL_NUM)) {
echo "$test[0]\n";
}

Is my PHP somehow broke, or am I just missing something here?
Thanks.

Scott Nipp
Phone: (214) 858-1289
E-mail: sn4265@sbc.com
Web: http:\\ldsa.sbcld.s­bc.com

--
PHP Database Mailing List (http://www.php.net­/)
To unsubscribe, visit: http://www.php.net/­unsub.php

--
PHP Database Mailing List (http://www.php.net­/)
To unsubscribe, visit: http://www.php.net/­unsub.php


Add comment
 

Add new comment

As:
Login:  Password:  
 
 
  
 
Пожалуйста, относитесь к собеседникам уважительно, не используйте нецензурные слова, не злоупотребляйте заглавными буквами, не публикуйте рекламу и объявления о купле/продаже, а также материалы нарушающие сетевой этикет или УК РФ.


QAIX > PHP web-programming > Problem with foreach looping... 8 April 2005 00:31:22

see also:
Skip header rows with sql loader
www.dvd100.net El mejor precio en…
Help with combo boxes in forms
пройди тесты:
see also:
Extol for you
newsleecher
DOBRODOLI - WELCOME - WILLKOMMEN…

  Copyright © 2001—2008 QAIX
Idea: Miсhael Monashev
Помощь и задать вопросы можно в сообществе support.qaix.com.
Сообщения об ошибках оставляем в сообществе bugs.qaix.com.
Предложения и комментарии пишем в сообществе suggest.qaix.com.
Информация для родителей.
Write us at:
If you would like to report an abuse of our service, such as a spam message, please .