Solved

PHP Tokenizer and SQL

Posted on 2004-10-11
5
460 Views
Last Modified: 2008-03-17
Hi there,

I am looking at writing a simple SQL interface.

I want to tokenize an SQL string so I can prepend a table identifier to table names.

The reason is I only have one database, and I want to separate out the tables so they have structure...

for instance

userlist for the blog section should be different to the userlist for the distribution list section, but theyh could have simple sql...

select * from userlist

with a tokenizer, I can prepend the DB section to the table name..

select * from dist_userlist
select * from blog_userlist

making reading a lot simpler... I think.

Any ideas how I can do this simply?

Thanks
Nigel.
0
Comment
Question by:nigel5
  • 2
  • 2
5 Comments
 
LVL 49

Expert Comment

by:Roonaan
Comment Utility
substr(strtolower($query), ' from ');
substr(strtolower($query), ' where');

You'd then good retrieve the 'from table table table where'-piece and replacement should be doable.

Or even better. Have variable queries:

$query = 'SELECT * FROM `'.$table.'` WHERE etc';

-r-
0
 
LVL 3

Accepted Solution

by:
gnudiff earned 125 total points
Comment Utility
I wrote a small class for that recently.

The basic idea is to store different parts of the query in separate arrays, and only join them into a query upon executing it.

ie. something like:

$sb = new SQLBuilder;

$sb->add_column("mycol");
$sb->add_clause('FROM', 'table1 t1');
$sb->add_clause('FROM', 'table2 t2');
$sb->add_clause('WHERE', 't2.id = t1.id');
etc.

and then $query = $sb->parse();

where parse takes all the currently defined parts together and forms a single:
SELECT mysql FROM table1 t1, table2 t2 WHERE t2.id=t1.id

0
 

Author Comment

by:nigel5
Comment Utility
roonaan. I had a few issues with strtolower() since the WHERE Clauses are generally case sensitive. I did think about finding the strpos of 'FROM' and 'WHERE' then using split.... but that got messy.

gnudiff, thanks that inspired me, and I have a class that builds the SQL in the first place. This is not the optimal solution however, since it is easier to type SQL.... it does mean that I cannot type SQL into a form input and have it run against the database...

But then would I really want that much power... best have some form with some control and dynamic inputs...

Thanks guys.
0
 
LVL 3

Expert Comment

by:gnudiff
Comment Utility
It certainly depends on your users.

I have just written some PHP that allows user to build query using webform and adding/removing conditions one by one.
Like, he can select dropdown for logical operator (AND/OR/AND NOT/OR NOT), field, condition (is/isn't/>/</contains/...) and value, and that gets shown in the list on the page, eg:

AND title contains 'One'
AND creation date > 2002-01-01
AND NOT header is 9338
AND NOT header starts with 7

There are certain benefits to storing the query in this form at all times, and constructing the real SQL only at the point of executing it.
0
 
LVL 49

Expert Comment

by:Roonaan
Comment Utility
Why I used strtolower was because you would like to have a case insensitive search to where the positions of the keywords 'FROM' and 'WHERE' are. This is because stripos is still not functional on all systems.

But better indeed to have a class which performs some validation before querying. Saves a lot of work in the future :-)

Regards

-r-
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now