Page 1 of 1

MySQL query using PHP

Posted: Sat Apr 03, 2004 8:48 am
by mjseaden
Dear All

Is there a form of MySQL query I can implement using mysql_query() that will allow me to count the number of fields in a database table, and their corresponding titles?

Many thanks

Mark

Posted: Sat Apr 03, 2004 8:52 am
by malcolmboston
to get the number of fields

Code: Select all

$query = "SELECT * FROM somewhere";
$result=mysql_query($query) or die(mysql_error()));
$num_fields=mysql_num_fields($result);

Posted: Sat Apr 03, 2004 8:56 am
by malcolmboston
i dont fully understand what you mean when you say 'titles' but im guessing this is what you want.....

Code: Select all

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

$fields = mysql_list_fields("your_database", "your_table", $link);
$columns = mysql_num_fields($fields);

for ($i = 0; $i < $columns; $i++) {
    echo mysql_field_name($fields, $i) . "\n";
}

Posted: Sat Apr 03, 2004 8:57 am
by mjseaden
That's correct. Thanks very much mb..

Posted: Sat Apr 03, 2004 9:12 am
by mjseaden
What I'd like to do is program my admin section, which needs to edit a large number of database tables, many interrellated, so that it can read a database table from the server, and accordingly construct an HTML table in standard form to display the field contents. There would be a few columns I would wish to hide from the user, which I could hide using an '_' character at the beginning of the field name to indicate the column should not be displayed.

The simplest way to do this would be to count the number of displayable columns and then carry out a simple division to determine an equal pixel width for each of the columns, however inevitably some data fields contain text or numbers a lot greater in length than others. It would therefore be best to implement a 'best fit' system, so that columns with data generally longer in (pixel) length than others would get a greater column width share than those with, for example, just single figure numbers which would have very short pixel lengths.

Is there any intuitive way I can implement a 'best fit' system that divides column width proportions accordingly?

Many thanks

Mark