What characters have to be escaped to prevent (My)SQL injections?

前端 未结 6 820
温柔的废话
温柔的废话 2020-11-29 05:47

I\'m using MySQL API\'s function

mysql_real_escape_string()

Based on the documentation, it escapes the following characters:



        
6条回答
  •  萌比男神i
    2020-11-29 05:53

    Java solution:

    public static String filter( String s ) {
        StringBuffer buffer = new StringBuffer();
        int i;
    
        for( byte b : s.getBytes() ) {
            i = (int) b;
    
            switch( i ) {
                case  9 : buffer.append( "    " ); break;
                case 10 : buffer.append( "\\n"  ); break;
                case 13 : buffer.append( "\\r"  ); break;
                case 34 : buffer.append( "\\\"" ); break;
                case 39 : buffer.append( "\\'"  ); break;
                case 92 : buffer.append( "\\"   );
    
                if( i > 31 && i < 127 ) buffer.append( new String( new byte[] { b } ) );
            }
        }
    
        return buffer.toString();
    }
    

提交回复
热议问题