Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying SQL Server from PHP

I've been searching this topic, but haven´t found any solution; there was a similar question, but it is not solved: Working with characters with accents in sql query and table name

I'm working with a database on a SQL Server 2005 that uses Modern_Spanish_CI_AS as collation. I'm developing a web front-end using PHP, and I am stuck with an accent problem.

There's a table (let's say "Objetos") whose fields are: ID Name Descripción

I can´t query this table because of the accent. If I build my query "Select a.ID, a.Name FROM Objetos" it works perfect, but if changed into "Select a.ID, a.Descripción FROM Objetos" it returns zero rows.

I can´t change the collation of the database, so I have been decoding the queries before sending them $ssql = utf8_decode($ssql);... It is strange, using this workaround I can do INSERT, but cant SELECT.

Any ideas? Thanks

like image 468
Pete Avatar asked Mar 22 '13 08:03

Pete


People also ask

Can I use SQL Server with PHP?

The Microsoft Drivers for PHP for SQL Server enable integration with SQL Server for PHP applications. The drivers are PHP extensions that allow the reading and writing of SQL Server data from within PHP scripts.

What does Sqlsrv_query return?

Return Values ¶ Returns a statement resource on success and false if an error occurred.

Can I use array in SQL?

Arrays can be used only in the following contexts: Parameters to SQL functions. RETURN data types from SQL functions. Parameters to SQL procedures.

Is there an array in SQL Server?

Conclusion. As you can see, SQL Server does not include arrays. But we can use table variables, temporary tables or the STRING_SPLIT function. However, the STRING_SPLIT function is new and can be used only on SQL Server 2016 or later versions.


1 Answers

Try surrounding the column with the "`" character. If it doesn't work, try removing the "a." prefix from the column name.

like image 114
Clay Freeman Avatar answered Oct 04 '22 21:10

Clay Freeman