You are here: Re: [PHP] Splitting Vars from MySQL in PHP « PHP « IT news, forums, messages
Re: [PHP] Splitting Vars from MySQL in PHP

Posted by Rasmus Lerdorf on 05/01/05 06:16

Richard Lynch wrote:
> On Fri, April 29, 2005 4:36 pm, Philip Olson said:
>
>>>>I remember in Perl I used to extract vars from a single fetchrow by
>>>
>>>adding
>>>
>>>>each var name to the beginning (like this). Only this ain'ta workin
>>>
>>>:)...
>>>
>>>>Anyone know the right syntax to do this?
>>>>
>>>>($var1, $var2, $var3)= mysql_fetch_array($result, MYSQL_ASSOC)
>>>
>>>
>>>list($var1, $var2, $var3) = mysql_fetch_array($result, MYSQL_ASSOC);
>>>http://us4.php.net/manual/en/function.list.php
>>
>>But remember that list() only works with numerical
>>arrays so use MYSQL_NUM (or array_values()) in the
>>above. For associative, extract() can be useful.
>
>
> I dunno what the documentation is trying to say when it says "list only
> works with numerical indices" but it's patently false:
>
> php -a
> <?php
> $foo = array('a'=>'apple', 'b'=>'banana', 'c'=>'coconut');
> while (list($k, $v) = each($foo)){
> echo "$k: $v\n";
> }
> ?>
> Content-type: text/html
> X-Powered-By: PHP/4.3.11
>
> a: apple
> b: banana
> c: coconut
>
> It has worked just fine with alpha indices since PHP 3.0rc2, at a minimum,
> cuz I've been doing it that long.
>
> I dunno who was smoking what that day they typed the documentation :-)
>
> I'm pretty sure it's not an "undocumented feature" that it works.

Richard, this is what the docs are talking about:

list($a,$b,$c,$d) = array('a','b','c','d');
echo $a,$b,$c,$d;

vs.

list($a,$b,$c,$d) = array('a'=>'a','b'=>'b','c'=>'c','d'=>'d');
echo $a,$b,$c,$d;

In your case you are calling each() which itself returns a numerically
indexed array.

-Rasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация