Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:17:33+00:00 2026-06-07T16:17:33+00:00

I am converting some of my code from the older mysql extension to the

  • 0

I am converting some of my code from the older mysql extension to the mysqli extension in PHP. Previously, with the mysql extension, I had used some code like this to find the primary key in a table:

while ($i < mysql_num_fields($result)) {
    $meta = mysql_fetch_field($result, $i);
    if ($meta->primary_key == 1){ 
        $primary_key = $meta->name; 
    }
    $i++;
}

$meta->primary_key == 1 was very convenient.

So far I have converted to code to using mysqli:

while ($i < $result->field_count) {
    $meta = $result->fetch_field;
    if ($meta->primary_key == 1){ 
        $primary_key = $meta->name; 
    }
    $i++;
}

Of course, from looking at the docs here we can see that $meta->primary_key doesn’t exist in mysqli. I see that there is a $meta->flags. This is my best guess, although i am not sure of what value flags should be when I have a primary key.

Does anyone know how I tell which column is the primary key for a table using mysqli?

Thanks!

EDIT
Here is some working code:

//get primary key
$primary_key = '';
while ($meta = $result->fetch_field()) {
    if ($meta->flags & MYSQLI_PRI_KEY_FLAG) { 
        $primary_key = $meta->name; 
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:17:34+00:00Added an answer on June 7, 2026 at 4:17 pm

    You were very close, you will need the flags property.

    The flag you are looking for is MYSQLI_PRI_KEY_FLAG, which means:

    Field is part of a primary index

    You can test for this flag with something like:

    if ($meta->flags & MYSQLI_PRI_KEY_FLAG) { 
      //it is a primary key!
    }
    

    You are using & here as a Bitwise AND Operator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some objective-c code I'm converting from iPhone to iPad. CFGregorianDate is used
I'm converting some c code to python. Can someone convert this for loop from
I am converting some Java code to C#. This code is using getGlyphOutline from
I am converting some code from Perl into .NET. I have this s/// pattern
I am manually converting code from Java (1.6) to C# and finding some difficulty
I am in the process of converting some code from C# to Java. I
I am having trouble converting some code from VB6 to VB.NET (I don't have
I'm converting some html parsing code from BeautifulSoup to lxml. I'm trying to figure
I have been converting some code from C++ to C#. My lack of understanding
I'm using mootools 1.4.3 for basic class inheritance. I'm actually converting some code from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.