Round Posted November 8, 2006 Share Posted November 8, 2006 I am trying to select column names from a table in our db that contain spaces, But I can't ???$sql = "select distinct grp no from tbl_studiag_num where stu no=\"$stu_id\" ";Which causes an error message.I have also tried:-$sql2 = "select distinct 'grp no' from tbl_studiag_num where 'stu no'=\"$stu_id\" ";Which doesn't cause the error message, but doesn't return any rows?!? But I know there are matching rows?!?I know its not good practice to give tables and columns, names containg spaces but I didn't create the DB and I can't rename them for obvious reasons.Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/26559-select-column-names-that-contain-spaces/ Share on other sites More sharing options...
MCP Posted November 9, 2006 Share Posted November 9, 2006 it will treat 'stu no' as a string, which probably isn't equal to any of your student idsuse square brackets instead: [stu no]On another note, I always wondered why it was 'bad practice' to use spaces? Other than a bit more typing for the SQL Coder... it's certainly more legible than CamelCode or less annoying than_using_underscores... Quote Link to comment https://forums.phpfreaks.com/topic/26559-select-column-names-that-contain-spaces/#findComment-121971 Share on other sites More sharing options...
Round Posted November 9, 2006 Author Share Posted November 9, 2006 Thanks MCP.I agree with you, why is it considered as "bad practice". For me I don't know, I just make sure I use underscores as I was told it's best to. The thing is, Yes it caused a problem for me this time, but if I'd been taught the spaces method it wouldn't have been an issue. However I'm not going to shoot the mesenger either because chances are the fairly useless underscores knowledge has probably been passed on and no one knows the actual reason for it. I agree if people want to say its so that there is a standard, but if no one used them the satndard would be no underscores and people using [..]. But one thing is for sure it is easier to type without the underscore because it's not even a single button press, it always requires shift.Cheers Quote Link to comment https://forums.phpfreaks.com/topic/26559-select-column-names-that-contain-spaces/#findComment-122016 Share on other sites More sharing options...
Vikas Jayna Posted November 9, 2006 Share Posted November 9, 2006 In [b]mysql[/b], you can enclose the column names within [b]backquotes[/b] like this:[code]$sql2 = "select distinct `grp no` from tbl_studiag_num where `stu no`=\"$stu_id\" ";[/code]and it'll work.cheers :) Quote Link to comment https://forums.phpfreaks.com/topic/26559-select-column-names-that-contain-spaces/#findComment-122061 Share on other sites More sharing options...
Round Posted November 9, 2006 Author Share Posted November 9, 2006 Nice 1, I'm eventually going to upload this site to our main server and it will be querying our main db.Which method will work with a microsoft db? (mssql) Quote Link to comment https://forums.phpfreaks.com/topic/26559-select-column-names-that-contain-spaces/#findComment-122071 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.