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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:40:51+00:00 2026-05-18T11:40:51+00:00

Before jumping in and rolling my own, I thought I’d ask in here first.

  • 0

Before jumping in and rolling my own, I thought I’d ask in here first.

I am looking to do some elementary parsing of random SQL commands in order to:

  1. inspect the field names that are being used in ‘SELECT’s (including any subqueries)
  2. inspect the field names that are being used in ‘JOIN’s (including any subqueries)
  3. inspect the names of tables being used in the query (including any subqueries)

I have seen some SQL parser classes out there, but they are far too ‘heavyweight’ for the use cases described above.

Is anyone aware of a lightweight class that has atleast some of the required functionality?

Worst case scenario, if I have to write a parser, what would be the best way of writing such a parser (normally, to write a parser, I would normally resort to tools that are not available in PHP), any tips on how to write a ‘rough and ready’ class to do this parsing?

//rough sketch
<?php
class SqlParser()
{
    protected $sqlstr;
    protected $m_tablenames = array();
    protected $m_fieldnames = array();

    public function __construct($sql){
       $this->sqlstr = $sqlcmd;
       $this->parseString($sqlstr);
    }

    public function __destroy(){}
    public function getTableNames(){ return m_tablenames; }
    public function getFieldNames(){ return m_fieldnames; }

    private function parseString($sql)
    {
        //TODO
    }
}

?>

I would prefer the parsing to be SQL dialect agnostic (i.e. not tied to any particular SQL dialect or db specific SQL) as much as possible.

If that is not possible, then the SQL dialect I will be using is PostgreSQL.

  • 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-05-18T11:40:52+00:00Added an answer on May 18, 2026 at 11:40 am

    PHP SQL Parser might be what you’re looking for. It’ll handle fairly complex queries, as you can see from the link. Download the code from the projects front page. The only drawback is it targets MySQL only. Adding support for PostgreSQL should be no big problem.

    There’s also a more basic solution for SQL parsing: PHP SQL Tokenizer, but it does not offer you anything but select/from/where/order separation: no field names, subquery extraction, or such.

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

Sidebar

Related Questions

Thought Id ask here before jumping into a problem on the Blackberry Playbook (Adobe
I'd like to learn some Javascript before jumping into a framework like jQuery or
Before I do this I figured I would ask if it was the best
Before you start firing at me, I'm NOT looking to do this, but someone
Before I read this article , I thought access control in Ruby worked like
Before looking into my question, lets consider the fopen in C. There are multiple
First of all, I realize I should have tried to fully grasp Ruby before
I'm new to iPhone developement and I did a lot of reading before jumping
Before I plan to apply some algorithm, wanted to clarify, whether easier solution is
Before you answer this I have never developed anything popular enough to attain high

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.