SQL Query Table Name Space

Solved
Giuliano -  
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   -

Hello everyone,

I would like to make a query to display data from my database on a website. Unfortunately, some table names contain spaces, and I don't know the correct syntax to have the table recognized...

I know what you’re going to say, not to use spaces in table names!! Unfortunately, I didn't create this database and considering its size, it would be impossible for me to rename all the tables...

Could someone help me? My database is Access and I'm programming in PHP on my site.

Thanks in advance!

6 answers

adnanos
 

It's simple, use backquotes, for example `your field` ;)

Be careful not to use apostrophes ' ' !!!!!

100%

18
epaude
 
Thank you, it's working.
0
Incognito
 

Hello. This method doesn't work; putting quotes around it only returns a column of strings containing the name of the initial column. For example, SELECT 'Code secteur' FROM Table returns 'Code secteur' as the column name, then the fields: 'Code secteur', 'Code secteur', 'Code secteur', etc., on each line. However, it is clear that we are looking to obtain the values of the 'Code secteur' column and not its name itself.

If anyone has a solution for this, thank you in advance.

0
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830 > Incognito
 

Hello

It is clearly stated that you should not use quotes (apostrophe) or double quotes (quotation marks).. but backquotes... ` (the inverted apostrophe)

And if it's an Access database you should be able to use brackets. [your field] it seems to me

1
Giuliano
 
Does anyone have a little idea to help me out? I really don't know what to do anymore... Thank you in advance!
3
AC
 
Hi!
I don't think there's a solution to your problem, generally PHP is used to link with a MySQL or similar type database and space is a forbidden character in it. Always try to enclose your table names with ` (Alt GR + 7) but don't get your hopes up too much. In my opinion, you'll need to rename your tables.
3
Giuliano
 
Hi, first of all, thank you for your response even if it's not necessarily the one I was expecting... ;o)
Renaming the tables is going to be difficult because there are a lot of them!!! And maybe doing it with another language? Java, ASP?!
I'll look into all of this, and if I can't find another way, I will tackle the task of renaming all my tables...
Thanks anyway!
0
gwaa Posted messages 1 Registration date   Status Member
 
It seems to me that phpMyAdmin (which is written in PHP) allows you to create and use MySQL tables with a name containing a space character, so it must be possible...
0
babbel > gwaa Posted messages 1 Registration date   Status Member
 
Hi,
Well, it's been a while since you posted your message, but I stumbled upon it by chance because I'm having the same issue but on SQL Server.

For you and others who might have the same problem, the query works fine if you enclose the name of your table in [ ].

So in my case, it looks like this:
select * from [Current Month]

@+
1
noumidya > babbel
 
I thank you infinitely for the trick of [] to use in the WHERE clause for columns that contain names separated by a space. You have saved me a huge amount of work renaming everything from the start.
Thanks again, my friend.
0
Pierre
 
Otherwise, if you work with PHP and MySQL, simply enclosing the table names with ` in the query is enough for it to work.

See you, Pierre
0
DragonMiki
 
Yes, it is possible to work with MySQL and tables or fields with spaces, even if it's not ideal.
For that, make a small example in PhpMyAdmin with an insertion or a select, it will give you the syntax

example for an insertion:

INSERT INTO `table with space` ( `id` , `width` , `height` , `thickness` , `field name with space`)
VALUES ('$id', '$width', '$height', '$thickness', '$field_name')

Because it is not always easy or even allowed to rename the names of tables or fields, and the person who created the table or the fields did not necessarily do everything as it should be :)
0
-CCM-
 
Hello, in the same vein, with PHP and MySQL.

I retrieve document names that will serve as URLs.
The problem is that some document names contain spaces.

So when I retrieve the name and store it in my database, I have: "document name.pdf"
But when I use it in a script to create a link, I simply get: "http://localhost/name"

It is necessary to rename the document to: "document%20name.pdf"
To have the URL: "http://localhost/document%20name.pdf"

My variable $nom contains: "document name.pdf"
But when writing: "http://localhost/`$url`" I get: "http://localhost/%60name"

How can I get a variable that will serve as a URL containing spaces?

Thank you
0
simo
 
tt simply [ field name] it's practical
0