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
Selecting the primarykey w/o knowing the fieldname or index
Moderator: General Moderators
-
penguinboy
- Forum Contributor
- Posts: 171
- Joined: Thu Nov 07, 2002 11:25 am
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
-
penguinboy
- Forum Contributor
- Posts: 171
- Joined: Thu Nov 07, 2002 11:25 am
-
penguinboy
- Forum Contributor
- Posts: 171
- Joined: Thu Nov 07, 2002 11:25 am
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;