Icebergness Posted May 4, 2012 Share Posted May 4, 2012 Hi, I am trying to connect to our company software database via PHP. I've got the connection working and can retrieve records etc. However one of my tables has a ridiculous column name that I can't get working. Here's the code... $mssql_server = "sql-primary"; $connectionInfo = array( "Database"=>"FOUR_I_CORE", "UID"=>"...", "PWD"=>"..."); $connnection = sqlsrv_connect( $mssql_server, $connectionInfo); $sql = "SELECT [REF (SEDOL) No.] FROM [sTO_CORE] WHERE [REF (SEDOL) No.] = $sedol"; $stmt = sqlsrv_query( $connnection, $sql ); if( $stmt === false) { die( print_r( sqlsrv_errors(), true) ); } while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) { //echo '<b>Client Ref:</b> ' . $row['REF (SEDOL) No.'] . '<br>'; } I keep getting the following error message: Array ( [0] => Array ( [0] => 42S22 [sqlSTATE] => 42S22 [1] => 207 [code] => 207 [2] => [Microsoft][sql Server Native Client 10.0][sql Server]Invalid column name 'B7N4L15'. [message] => [Microsoft][sql Server Native Client 10.0][sql Server]Invalid column name 'B7N4L15'. ) ) FYI, B7N4L15 is the value being entered as $sedol. I'm pretty sure the problem is with the column name REF (SEDOL) No., but I've no idea how to contain the dodgy name. Unfortunately, I don't have the luxury of renaming the column, so I have to make do. Is there anything I can do to make this work? Cheers Link to comment https://forums.phpfreaks.com/topic/262068-dealing-with-stupid-column-names/ Share on other sites More sharing options...
trq Posted May 4, 2012 Share Posted May 4, 2012 Strings require quotes in SQL. $sql = "SELECT [REF (SEDOL) No.] FROM [sTO_CORE] WHERE [REF (SEDOL) No.] = '$sedol'"; Link to comment https://forums.phpfreaks.com/topic/262068-dealing-with-stupid-column-names/#findComment-1343009 Share on other sites More sharing options...
Icebergness Posted May 8, 2012 Author Share Posted May 8, 2012 *facepalm* how stupid do I feel now? That solved it for me so thank you very much, you saved my bacon! Link to comment https://forums.phpfreaks.com/topic/262068-dealing-with-stupid-column-names/#findComment-1343888 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.