in reply to variables inside SQL statements

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
RE: Answer: variables inside SQL statements
by johannz (Hermit) on Oct 11, 2000 at 01:27 UTC

    The danger with this approach is it is up to the developer to ensure that the interpolated string is a valid SQL command. This is particularly dangerous when the valid you wish to store contains single quotes since it the escape mechanism is Database-dependent. It is far simpler to use DBI and the '?' (question mark) syntax to specify variable substitution at execution time. This method will take care of escaping any required data for you.?

    my $path = '/path/to/this'; my $sql = 'INSERT INTO table SET ( path ) VALUES ( ? )'; my $STH = $DBH->prepare($sql); $STH->execute($path);
      I would have been happy to offer that answer but I was given no sound proof that the asker was using DBD.

      Update:I hope that -- was for a sound reason. There wasn't even evidence that the user was using a database that DBI can connect to.

      --
      Casey
         I am a superhero.