doucheng4660 2013-08-20 08:03
浏览 103

在PHP中使用params调用SQL Server存储过程

this is my 1st post here and as a beginner in PHP so please tread lightly!!

I am using PHP 5.3 and SQL Server 2012 express

My problem is executing an SQL server stored procedure from PHP that has parameters where the value of the parameter comes from a $_POST variable from an HTML form.

My PHP code is as follows:

    <?php
    require_once("../includes/initialize.php");
    //
    global $database;


    $username = "someone";
    $params = array($username); 
    $conn = $database->connection;

    $admin_set = sqlsrv_query($conn, "{call find_admin_by_username_p}", $params); 
    if($admin = $database->fetch_array($admin_set)) {
        var_dump($admin);
    } else {
        return null;
    }
    //
    /* THIS WORKS
    global $database;

    $admin_set = $database->query("{call find_admin_by_username}");
    if($admin = $database->fetch_array($admin_set)) {
        var_dump($admin);
      } else {
        return null;
    }
    */
    ?>

The code for the stored procedure is:

    CREATE PROCEDURE [dbo].[find_admin_by_username_p]
@username nvarchar(55)
    AS
    BEGIN
SELECT * FROM dbo.users
WHERE username = @username
    END

For the commented "THIS WORKS" section the procedure find_admin_by_username works as this doesn't contain any parameters. However I want to have the @username as a variable that is provided by a $_POST form field.

My main reason for doing this is to help prevent SQL injection, also I would be escaping the values once connected to the form.

This is a link to a lot of the info I have looked at so far

http://blogs.msdn.com/b/brian_swan/archive/2011/02/16/do-stored-procedures-protect-against-sql-injection.aspx

I have also searched through many other blogs but don't seem to be able to find anything that works.

Please help!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型