SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting name of table INSERT queries

    Hey, I'm looking for a way to get the name of the table an INSERT-query worked on. I got the query's result (from mysql_query) and the string. I concidered doing string searches and narrowing it down between either "INSERT INTO " and " VALUES" or " (", but that'll go horribly wrong if the table name has " VALUES" in it, or if it's got a " (" in it. They're all possible (I tested that), which leaves string searching out as an option. Anybody here know how to get the table name then?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why would you ever run a query that your own program didn't produce?

    Look for the word after INSERT INTO (delineated by spaces and potentially backticks).

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to make an object that handles queries, and thought it'd be useful to allow it to hand back the inserted record (or updated record in case of updates).

    The problem with looking for the word after INSERT INTO is that a table can just as well be called "My Superduper Table (yes I know it is seriously cool)". Because of that, it's hard to define where exactly a table name stops.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It's not; it would have to be enclosed in backticks which tells you exactly where it starts and stops. If there are no backticks, then you only need to look for spaces.

    INSERT INTO My Superduper Table (yes I know it is seriously cool) VALUES (...)

    is not a valid query; MySQL would barf after the word My

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ohhhh, right. Thanks

  6. #6
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Keep in mind that the following are also valid (in MySQL anyway)
    Code SQL:
    INSERT DELAYED INTO `table_name` ...
    INSERT INTO DATABASE.my_table ....

    Might be more trouble than it's worth.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •