mysql_real_escape_string link tanımlayıcı önemi ()

3 Cevap php

Neden mysql_real_escape_string () kullanırken bir giriş olarak bir bağlantı tanıtıcısı olması gereklidir. Ben fonksiyon MySQL sorgu ile kullanılması gerektiğini biliyorum, ama işlevi gerçekten sadece bir dize değiştirmedir.

3 Cevap

Dan the PHP manual:

O () bir mysql_query yerleştirmek için güvenli olduğunu bu nedenle dikkate bağlantının geçerli karakter kümesini alarak unescaped_string özel karakterler kaçar. İkili veri eklenir edilecekse, bu fonksiyon kullanılmalıdır.

ve:

Link tanımlayıcı belirtilmemişse mysql_connect tarafından açılan son bağlantı () varsayılır. Böyle bir bağlantı bulunursa, mysql_connect () argüman hiçbir ile çağrılmış gibi bir bağlantı oluşturmaya çalışacağız. Hiçbir bağlantı veya kurulmuş ise, bir e_warning seviyesinde bir hata üretilir.

Bir bağlantı karakter kümesini belirlemek için gereklidir.

http://ilia.ws/archives/103-mysql_real_escape_string-versus-Prepared-Statements.html bir okuma var

For example, if GBK character set is being used, it will not convert an invalid multibyte sequence 0xbf27 (¿’) into 0xbf5c27 (¿\’ or in GBK a single valid multibyte character followed by a single quote). To determine the proper escaping methodology mysql_real_escape_string() needs to know the character set used, which is normally retrieved from the database connection cursor.

Maybe as a consequence of this article but clearly after it the function mysql_set_charset() has been added to the mysql extension. The charset is a property of the mysql connection (resource).
If you have multiple connections you really should pass them to mysql_real_escape_string() (and always use mysql_set_charset() instead of SET NAMES).
If you don't pass the connection resource the function will use the last connection established by your script. If the charset of the two (or more connections) differ the result of real_escape_string() may be misinterpreted by the server (expecting another encoding and therefore different escaping rules).
And since it doesn't hurt to do so even if you have only one connection (can you say with absolute certainty that this will be so until the end of time?) just pass it always.