MySQL query using 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

MySQL query using PHP

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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);
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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";
}
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

That's correct. Thanks very much mb..
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post 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
Post Reply