You are here: Re: How do I fetch an array of all field names in a mysql database? « All PHP « IT news, forums, messages
Re: How do I fetch an array of all field names in a mysql database?

Posted by robert on 09/28/34 11:48

<guttyguppy@gmail.com> wrote in message
news:1148586513.290292.272550@j73g2000cwa.googlegroups.com...
| Thanks for the lead-the manual is so huge and tough to read that I gave
| up the first time, but now I think I've got it:
|
| $query="SELECT * FROM my_table";
| $result=mysql_query($query);
| $i = 0;
| while ($i < mysql_num_fields($result)) {
| $meta = mysql_fetch_field($result, $i);
| echo $meta->name."</br>\n";
| $i++;
| }
|
| Is that good, non-depracated code though?

while it will work, it is certainly not as efficient as it could be. you
should never eval with a function if the return value isn't expected to
change...meaning, only do it when you absolutely have to. notice the added
where clause...since you only want field names and don't need any data
(based on your example).

$sql = "SELECT * FROM my_table WHERE 1 = 2";
$records = mysql_query($sql);
$numFields = mysql_num_fields($records);
for ($i = 0; $i < $numFields; $i++)
{
$field = mysql_fetch_field($records, $i);
echo $field->name . "<br />\r\n";
}

 

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

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