Selecting the primarykey w/o knowing the fieldname or index

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Selecting the primarykey w/o knowing the fieldname or index

Post by penguinboy »

I need to select the primary key from a table in a mysql database without knowledge of the field name or index.

Can it be done?

Thanks for any help.


--pb
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you know which number column it is then you could use mysql_fetch_row() (or your databases equivalent).

Mac
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Post by penguinboy »

That won't help.
I have no knowledge about the table all I have is the table name and database name.
Thanks for trying though.
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Post by penguinboy »

I got the answer from another forum.

Code: Select all

$sql = mysql_query('SHOW FIELDS FROM `your_table`');
while($row = mysql_fetch_array($sql))
{
	if($rowї'Key'] === 'PRI')
	{
		$primarykey = $rowї'Field'];
	}
}

echo $primarykey;
Post Reply