Re: How do I fetch an array of all field names in a mysql database?



In message <X8odg.2002$m61.581@xxxxxxxx>, robert
<ab@xxxxxxxxxxxxxxxxxxxxxxx> writes

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

$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";

--
Martin Jay
.



Relevant Pages