Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to detect primary key in a table using PHP

Status
Not open for further replies.

ulag

Programmer
Sep 4, 2001
23
US
I use mysql. I want to know what fields are primary or unique keys? I need to find by PHP. Please help me.

Thanks.

ulag
 
There's probably a simpler way to do it, but this function works ok for me.
Code:
function get_primkey($table) {
    $result = mysql_list_fields($dbname,$table);
    if (!$result) {
        return false;
    }
    while ($field = mysql_fetch_field($result)) {
        if ($field->primary_key) {
            $pri_key = $field->name;
            return $pri_key;
        }
    }
}
Matt
matt@paperlove.org
If I can help, I will.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top