Jump to content

Get Mysql column names?


Recommended Posts

Hey everyone


I'm making a php-script that I can use to access any Mysql table i use. That's why I don't know the names of the columns. Sometimes it's a table containing new, sometimes url-links to pictures. The script outputs an xml file to flash, and I usually do something like this:



        // Database iinformation

	mysql_pconnect($db_host, $db_user, $db_password);

// Select data in database
$result = mysql_query("select * from news");

// Loop through it
while ($row = mysql_fetch_array($result))


But when I don't know how many columns I have in my table or what their names are, I cannot figure it out.

What I want to do in my loop is something like this:

// Loop through it
while ($row = mysql_fetch_array($result))



Link to comment
Share on other sites

string mysql_field_name ( resource result, int field_offset )


/* The users table consists of three fields:
*   user_id
*   username
*   password.
$link = @mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
   die('Could not connect to MySQL server: ' . mysql_error());
$dbname = 'mydb';
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
   die("Could not set $dbname: " . mysql_error());
$res = mysql_query('select * from users', $link);

echo mysql_field_name($res, 0) . "\n";
echo mysql_field_name($res, 2);

The above example will output:




and use int mysql_num_fields ( resource result ) for a field count

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.