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 05/26/06 16:34

| >$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";
| >}
|
| You should be ashamed of yourself, Robert. That is wrong, wrong, wrong.
| It relies on "magic numbers," which you've previously described as
| "NONSENSE, "STUPID programming," and "amature." [Sic]
|
| Like many examples you post here, your code is bloated, overly
| complicated, and poor.
|
| IMO a much neater solution would be:
|
| $result=mysql_query("DESCRIBE $mysql_table");
| while ($row = mysql_fetch_array($result))
| echo $row['Field']."<br>\r\n";
|

first, my example is in keeping with the op's methodology...so that he
"get's it" through comparison.
second, how is my code "wrong x 3"?
third, where are the "magic numbers"? $i is an index counter and is REQUIRED
by mysql_fetch_field.
forth, your "IMO" solution is exactly "IMO". even still, you should
formalize your writing so that the "while" has loop body brakets. any arg
you make against that will be met with some doozy code i'd like you to
interpret that uses your style...like quadruple, single line nested
for/foreach's amongst multinested if's.
fifth, not all db's HAVE a "DESCRIBE" keyword or functionality...i write
from experience so that when i say "select * from table where 1 = 2", i know
it will return results - mysql or not.
sixth, i post code that answers questions instead of berating people with
things like this response: "1 - 1 = 0 :^)"...then again, you are (as noted
before and maintained now) as useful as a wort...just not as smart!

now, if you'd like to show me some examples of my "bloated" code, go
ahead...i'm festidious when it comes to maintenance and brevity of code, so
i'll be waiting. it would also be in your favor to post *your* own code that
shows the reduction of bloat and accomplishes the same task as the code you
are berating.

until then, m.j., FOAD

 

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

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