Page 1 of 1

Selecting the primarykey w/o knowing the fieldname or index

Posted: Wed May 21, 2003 4:10 pm
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

Posted: Wed May 21, 2003 4:43 pm
by twigletmac
If you know which number column it is then you could use mysql_fetch_row() (or your databases equivalent).

Mac

Posted: Thu May 22, 2003 9:48 am
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.

Posted: Thu May 22, 2003 10:27 am
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;