2
votes

I am trying to extract some data from a SQL Sever database via PHP using the Microsoft Drivers for PHP for SQL Server. Some tables I can retrieve fine but others give the following error message:

Array ( [0] => Array ( [0] => 42S02 [SQLSTATE] => 42S02 [1] => 208 [code] => 208 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid object name 'xyz$Permanent Assignments'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Invalid object name 'xyz$Permanent Assignments'. ) )

If I try and run the same statement from isql (SELECT * FROM [xyz$Permanent Assignments]) using the same credentials it works fine so I know that the statement is correct and the object does exist.

I have also tried with version 1 and 2 of the MS drivers.

The code is below but if I try other tables it works - it's all a bit hit and miss.

$tsql = "SELECT * 
         FROM [".$client."\$Permanent Assignments]";

print $tsql;

$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
     echo "Statement executed.";
} 
else 
{
     echo "Error in statement execution.
\n"; die( print_r( sqlsrv_errors(), true)); }

Anyone got any ideas?

Thanks

1
The tables that fail all have a $ in them, correct?Pekka
Hi No, all the tables have a $ in them. The format is [company name$table name], so [xyz$table a] will work and [xyz$table b] won't. Can't see a pattern. The tables are all from Navision.williamsdb
@william is it at all possible to show some working and not-working examples?Pekka
And I'm not a SQL Server expert, but are you 100% sure this is not a permissions issue? The current user has rights to access those tables?Pekka
Sure these work: $tsql = "SELECT * FROM [".$client."\$Base Calendar]"; $tsql = "SELECT * FROM [".$client."\$Customer]"; these do not $tsql = "SELECT * FROM [".$client."\$TS Assignment]"; $tsql = "SELECT * FROM [".$client."\$Permanent Assignments]"; and no I am not sure it is not permission! Looking at them again now the ones that fail are all from a Navision add on. So that would seem to be the likely cuplrit. Can't understand why the same username and password can select from them in isql but not from script though. Does the script run with different credentials?williamsdb

1 Answers

0
votes

Try quoting the table name with " instead of square brackets, if you use single quotes to delimit the string then you will not have to slash them out or slash out the $