473,505 Members | 13,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Valid MySQL database/table/column name regexp



Can anyone point me to a regular expression in PHP which could be used to
check that a proposed (My)SQL database/table/column name is valid, i.e.
shouldn't result in an SQL error when created?

The user of my (hopefully to be opensourced) program has the ability to
create database/table/column names on the fly.

I'm aware of obvious characters such as ., [space], things like >, etc.,
which won't work, but haven't been able to source a definitive list,
including having googled the MySQL site.

Obviously certain characters need to be filtered out, as noted above, but
I want to be as unrestrictive as possible; hence just [a-z]* isn't good
enough because things like _ are acceptable.

Ideally, I'd prefer a regexp that applies to _all_ vendors' databases, not
just MySQL as I'm about to migrate the program to being
database-independent, probably using PEAR DB, but even a MySQL-specific
regexp would do the job.

In the longer term, I plan some sort of entity conversion script so that
theoretically any character could be used, using some sort of escaping
mechanism probably.

Can anyone supply or suggest routes to such a regexp?
Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22
www.lucas-smith.co.uk

Senior Computing Technician (Web Technician)
Department of Geography, University of Cambridge (01223 3)33390

& Webmaster, SPRI
Scott Polar Research Institute, University of Cambridge
Jul 16 '05 #1
3 17336

On 15-Aug-2003, Martin Lucas-Smith <mv***@cam.ac.uk> wrote:
Can anyone point me to a regular expression in PHP which could be used to
check that a proposed (My)SQL database/table/column name is valid, i.e.
shouldn't result in an SQL error when created?

The user of my (hopefully to be opensourced) program has the ability to
create database/table/column names on the fly.

I'm aware of obvious characters such as ., [space], things like >, etc.,
which won't work, but haven't been able to source a definitive list,
including having googled the MySQL site.


http://www.mysql.com/doc/en/Legal_names.html
--
Tom Thackrey
www.creative-light.com
Jul 16 '05 #2
Tom Thackrey wrote:

On 15-Aug-2003, Martin Lucas-Smith <mv***@cam.ac.uk> wrote:
Can anyone point me to a regular expression in PHP which could be used to
check that a proposed (My)SQL database/table/column name is valid, i.e.
shouldn't result in an SQL error when created?


You could always do

if ($fieldname == adslashes($fieldname))

Jul 16 '05 #3
On Fri, 15 Aug 2003 18:44:56 +0100, Martin Lucas-Smith <mv***@cam.ac.uk> wrote:
Can anyone point me to a regular expression in PHP which could be used to
check that a proposed (My)SQL database/table/column name is valid, i.e.
shouldn't result in an SQL error when created?

The user of my (hopefully to be opensourced) program has the ability to
create database/table/column names on the fly.

I'm aware of obvious characters such as ., [space], things like >, etc.,
which won't work, but haven't been able to source a definitive list,
including having googled the MySQL site.
You can have spaces and all sorts of characters in a table and column name, if
you double quote it. But if you do so, they'd be a pain to work with. Sounds
like you're sensibly limiting it to valid identifiers that don't need quoting.
Obviously certain characters need to be filtered out, as noted above, but
I want to be as unrestrictive as possible; hence just [a-z]* isn't good
enough because things like _ are acceptable.

Ideally, I'd prefer a regexp that applies to _all_ vendors' databases, not
just MySQL as I'm about to migrate the program to being
database-independent, probably using PEAR DB, but even a MySQL-specific
regexp would do the job.
If you want something general, you're really best just sticking to:

[A-Za-z0-9_]+

In fact, it will be more restrictive than that; e.g. in Oracle an identifier
has to start with an alphabetic character (although it can be in Unicode...).

So perhaps:

[A-Za-z][A-Za-z0-9_]*

And then you have to watch out for reserved words, which differ between
databases, so you probably end up with a list of reserved words, and then a
regexp. And you also have to watch for the maximum length (64 in MySQL, 30 in
Oracle, others will vary).
In the longer term, I plan some sort of entity conversion script so that
theoretically any character could be used, using some sort of escaping
mechanism probably.

Can anyone supply or suggest routes to such a regexp?


--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2310
by: Spidah | last post by:
Is there an easier way to get the number of items in a database table than selecting all the records in it and then running mysql_num_rows on the result? Hamilton www.laughland.biz
2
5581
by: Elmar Grandel | last post by:
Hello, i just updated my mysql database from 4.0.x to 4.1 because i like to have the possibility to create subqueries. My problem with the new release is the following: In my running project i...
2
8574
by: Saiyou Anh | last post by:
I know passing table/column name as parameter to a stored procedure is not good practice, but sometimes I need to do that occasionally. I know there's a way can do that but forget how. Can someone...
5
3975
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
2
4233
by: samui | last post by:
I'm trying to get a php page to show data from a MySQL database. But the problem that I have is I don't know where to start. I'm very new at PHP and I'm constantly told by others that it's very...
1
9292
by: mahendravishwa | last post by:
Hi, I want to change DateTime format in MySQL Database Table in dd/MM/yyyy format. Its possible so please provide guidance. Thanks
0
1661
by: Andy B | last post by:
I need to take an existing GridView that connects to a database and gets rows from a table and force it to show x rows in the insert mode. This is what should happen: 1. There is a TextBox in...
4
3068
by: baked | last post by:
Hi, I am using an html form to put a text into a Mysql database table. Using PHP V5 When I recover and display only one space between each word is seen irrespective of how many I add. The...
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.