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条回答

    报告相同问题?

    悬赏问题

    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?