tags:

views:

93

answers:

4

I have a query

$sql ="SELECT CustomerID FROM tblCustomer 
WHERE EmailAddress = '".addslashes($_POST['username']) ."' AND Password = '".addslashes($_POST['password']) ."'";

//  while printing,   it will be

SELECT CustomerID FROM tblCustomer WHERE EmailAddress = 'test@ab\'c.com' AND Password = '123'

if we executing this in a mysql server it works, but not in a sql server

what is the solution for this? . Iam using sql server

+3  A: 

for mysql

USE mysql_real_escape_string

http://php.net/manual/en/function.mysql-real-escape-string.php

like :

// Query
$query = sprintf("SELECT * FROM tblCustomer WHERE user='%s' AND password='%s'",
            mysql_real_escape_string($user),
            mysql_real_escape_string($password));

for mssql

look on the answers here :

http://stackoverflow.com/questions/574805/how-to-escape-strings-in-mssql-using-php

Haim Evgi
Doesn't work for SQL server.
cHao
This works only for MySQL, not MS SQL Server. The poster appears to want a solution that works in MS SQL Server too.
Asaph
if we do like that, the query will output likeSELECT * FROM tblCustomer WHERE EmailAddress='' AND Password=''// form values are not outputing from mysql_real_escape_string
Linto P D
i edit the answer
Haim Evgi
I wish i could downvote this answer again. Your ms_escape_string function is the kind of stuff i pride myself on **not** doing -- the data should be validated by the caller, and if valid, should make it to the DB **unmodified**.
cHao
its example i link to the original question.
Haim Evgi
@haim evgi: And i downvoted the source of your example too. No quoting function should mangle data -- its whole purpose in life is to see that data makes it into the DB unaffected by the database's quote syntax.
cHao
+7  A: 

addslashes() will escape single quotes with a leading backslash which is valid syntax in MySQL but not in MS SQL Server. The correct way to escape a single quote in MS SQL Server is with another single quote. Use mysql_real_escape_string() for MySQL (mysql_escape_string() has been deprecated). Unfortunately, no analogous mssql_ function exists so you'll have to roll your own using str_replace(), preg_replace() or something similar. Better yet, use a database neutral abstraction layer such as PDO that supports parameterized queries.

Asaph
@Asaph: I'd +1 you, except that you linked to the same halfassed "quoting" function i just downvoted twice.
cHao
@cHao: I didn't look carefully enough at that function before I linked to it. I didn't notice it was mangling data. Thanks for pointing that out to me. I've removed the link to the offensive function from my answer.
Asaph
@Asaph: Much better. +1 :)
cHao
+2  A: 

For MySQL, you want to use mysql_real_escape_string. addslashes does almost the same thing and has fewer letters, but apparently it gets some stuff wrong -- don't use it.

For SQL Server, it's a bit more complicated, as (1) MySQL quotes stuff non-standardly, and (2) i don't see a function made to quote stuff for SQL Server. However, the following should work for you...

$escaped_str = str_replace("'", "''", $unsafe_str);
cHao
`addslashes()` was designed for a different purpose and gets one subtle case wrong. Don't use it for escaping for MySQL.
staticsan
thanks,it works
Linto P D
@staticsan: I kinda figured there was a reason there was a whole other function (with a deprecated previous version!) to quote MySQL strings. +1 for mentioning it. :) Edited my answer accordingly.
cHao
+1  A: 

You shouldn't really be building the SQL statement dynamically as it's dangerous (and unnecessary). The correct thing to do is to use a paramerised query see http://msdn.microsoft.com/en-us/library/cc296201%28SQL.90%29.aspx

$sql ="SELECT CustomerID FROM tblCustomer WHERE EmailAddress = ? AND Password = ?";
$stmt = sqlsrv_query( $conn, $sql, array($_POST['username'], $_POST['password']));

This is much safer and means you don't have to worry about escaping characters. Another thing is beware of case sensitive / insensitve comparisons. For example if you wanted email address to be case insensitive but password case sensitive use something like:

$sql ="SELECT CustomerID FROM tblCustomer WHERE EmailAddress = ? COLLATE SQL_Latin1_General_CP1_CIAI AND Password = ? COLLATE SQL_Latin1_General_CP1_CSAS";
Joel Mansford