Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 707505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:17:06+00:00 2026-05-14T04:17:06+00:00

I have recently taken on a project in which I need to integrate with

  • 0

I have recently taken on a project in which I need to integrate with PHP/SQL Server. I am looking for the quickest and easiest function to prevent SQL injection on SQL Server as I prefer MySQL and do not anticipate many more SQL Server related projects.

Is this function sufficient?

$someVal = mssql_escape($_POST['someVal']);

$query = "INSERT INTO tblName SET field = $someVal";

mssql_execute($query);

function mssql_escape($str) {
    return str_replace("'", "''", $str);
}

If not, what additional steps should I take?


EDIT:
I am running on a Linux server – sqlsrv_query() only works if your hosting environment is windows

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:17:07+00:00Added an answer on May 14, 2026 at 4:17 am

    The best option: do not use SQL statements that get concatenated together – use parametrized queries.

    E.g. do not create something like

    string stmt = "INSERT INTO dbo.MyTable(field1,field2) VALUES(" + value1 + ", " + value2 + ")"
    

    or something like that and then try to “sanitize” it by replacing single quotes or something – you’ll never catch everything, someone will always find a way around your “safe guarding”.

    Instead, use:

    string stmt = "INSERT INTO dbo.MyTable(field1,field2) VALUES(@value1, @value2)";
    

    and then set the parameter values before executing this INSERT statement. This is really the only reliable way to avoid SQL injection – use it!

    UPDATE: how to use parametrized queries from PHP – I found something here – does that help at all?

    $tsql = "INSERT INTO DateTimeTable (myDate, myTime,
                                        myDateTimeOffset, myDatetime2)
             VALUES (?, ?, ?, ?)";
    
    $params = array(
                date("Y-m-d"), // Current date in Y-m-d format.
                "15:30:41.987", // Time as a string.
                date("c"), // Current date in ISO 8601 format.
                date("Y-m-d H:i:s.u") // Current date and time.
              );
    
    $stmt = sqlsrv_query($conn, $tsql, $params);
    

    So it seems you can’t use “named” parameters like @value1, @value2, but instead you just use question marks ? for each parameter, and you basically just create a parameter array which you then pass into the query.

    This article Accessing SQL Server Databases with PHP might also help – it has a similar sample of how to insert data using the parametrized queries.

    UPDATE: after you’ve revealed that you’re on Linux, this approach doesn’t work anymore. Instead, you need to use an alternate library in PHP to call a database – something like PDO.

    PDO should work both on any *nix type operating system, and against all sorts of databases, including SQL Server, and it supports parametrized queries, too:

    $db = new PDO('your-connection-string-here');
    $stmt = $db->prepare("SELECT priv FROM testUsers WHERE username=:username AND password=:password");
    $stmt->bindParam(':username', $user);
    $stmt->bindParam(':password', $pass);
    $stmt->execute();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have taken over a .NET project recently and upon looking at the db
I have recently taken over a project in PHP/CakePHP . Now I wanted to
I have recently had a linux server compromised from bots uploading .php scripts and
I'm recently working on a project in which I need to port a portion
I've recently have taken a new server under my wing that has an interesting
Recently I have been looking into Cassandra from our new project's perspective and learned
I have recently taken the ABS4.0 dive. However, I appear to be having an
I have recently merged together 5 of my stand-alone projects into one project to
I have recently upgraded my MVC 3 project to MVC 4. After all the
I recently published my first MVC 2 project to a commercial web server, running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.