Reading column labels (or not) in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Reading column labels (or not) in PHP

Post by mjseaden »

Hi

I have a table that I have created from a table stored in an ASCII file. The columns have random labels (they could be anything), and so when I use

Code: Select all

$row = mysql_fetch_array($result);
I am not sure what to put in the quotes of $row['???'] in order to read the first, second, third column etc.

Finally, is there any way of determining a column's label given a column number?

Many thanks

Mark
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Hi - is there a way of returning a column's label, given a column number? I've discovered that I can obviously return a column element by using e.g. $row[1], however is there any way of returning the label of the array element (e.g. 1 = '????').

Many thanks

Mark
User avatar
AnarKy
Forum Contributor
Posts: 119
Joined: Tue Nov 02, 2004 1:49 am
Location: South Africa

Part of my export table script.

Post by AnarKy »

Hi, This ids part of a script i wrote to export a
MySQL table to a CSV file.

:idea: I think this extract is what you are looking for.

Code: Select all

<?php
        $rs = mysql_query($x_QUERY,$conn) or die(mysql_error());

        if (mysql_num_rows($rs) == 0 ) {
            exit();
        }
        $totalRecs = intval(@mysql_num_rows($rs));

        $count = mysql_num_fields($rs);
        for ($ii =0; $ii < $count ;$ii ++){
            $x_name = mysql_field_name($rs, $ii);
            echo " $x_name ".",";
            }
        echo"\n";

?>
Think this pretty much explains itself.
It performs a query, then finds the number of fields,
then iterates through them.

Hope this is what you need.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Yep, that's it. Thanks very much.

Mark
Post Reply