php - Run queries to MS SQL server with strange table names -
for application need connect sql server 2008 queries.
i run queries in php on codeigniter framework. access sql database program called "microsoft sql server management studio". tables have strange names dollar signs: trp$lease car
example.
the studio data query:
select top 1000 * [mdatabase].[dbo].[trp$lease car]
when run query in php installation fails on $
sign:
$data = $this->db->query("select * dbo.trp$lease car");
like this:
a php error encountered
severity: notice
message: undefined variable: lease
filename: views/welcome_message.php
line number: 5 database error occurred
error number: 42s02
[microsoft][sql server native client 11.0][sql server]invalid object name 'dbo.trp'.
how need call these tables when running query php?
edit
the problem appears on tables space in name... query trp$invoice
works. in case trp$lease car
problem think..
this due fact have query in double-quotes. when php encounters double-quoted string containing $something
, treat $something
variable , attempt substitute value. in case, have tpr$lease
in string - php attempts resolve variable $lease
- haven't defined - , gives warning message indicated. there 2 ways of dealing it.
escape dollar sign:
"select * tpr\$lease"
- telling php need dollar sign there.use single-quoted string:
'select * tpr$lease'
- php not variable substitution in single-quoted strings.
obviously, adjust actual sql statements according specific schema.
edit: if table name contains spaces, have use square brackets [ ... ]
around tabla name:
"select * [tpr\$lease car]"
note ms sql specific syntax. not work on other database engines, (to knowledge) ms allows spaces in table names.
Comments
Post a Comment