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 9133775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:32:44+00:00 2026-06-17T08:32:44+00:00

I’m just wondering if there’s any way to select a row using the primary

  • 0

I’m just wondering if there’s any way to select a row using the primary key in the WHERE clause without necessarily knowing the name of the key’s column.

My suggested PHP function is as follows:

function getData($table,$key,$asarray=false) {
    $sql = mysql_query("select * from `".$table."` where primary='".$key."'");
    if( !$asarray) return mysql_fetch_assoc($sql);
    $ret = [];
    while($row = mysql_fetch_assoc($sql)) $ret[] = $row;
    return $ret;
}

A few notes:

  1. $table only ever comes from literal strings inside the code, never from an outside source.
  2. For the purposes of this question, assume that $key is already properly escaped.
  3. I don’t care that mysql is deprecated in PHP 5.5, because I’m using PHP 5.4
  4. For that same reason $ret = [] is a perfectly valid way to define an array – I say this because in my last question people were telling me my code wasn’t working because of this syntax.

Now, my current, actual, working code has an associative array mapping table names to their corresponding primary key field names. I’m just wondering if there’s an automated way to do this, preferably one that doesn’t involve querying INFORMATION_SCHEMA, SHOW CREATE TABLE or SHOW INDEXES.

  • 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-17T08:32:45+00:00Added an answer on June 17, 2026 at 8:32 am

    No, there is no automated way of doing it without knowing or querying the primary key column(s).

    Note that the primary key may be more than one column!

    I implemented this sort of generic find-by-primary-key method in the Zend Framework 1.0. We had to write code for the table gateway class to discover the table’s primary key and all other columns.

    Use DESC as a faster alternative to SHOW CREATE TABLE or querying the INFORMATION_SCHEMA. The SHOW commands just map to queries against INFORMATION_SCHEMA, and that can have a high cost because it triggers InnoDB tables to re-read a portion of the tables to refresh in-memory statistics (unless you set innodb_stats_on_metadata=0).

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I know there's a lot of other questions out there that deal with this
I'm trying to select an H1 element which is the second-child in its group

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.