-1

i want to pass my params into stored procedure in SQL server but it seems like i can't do this nicely

this is my php code for executing procedure in SQL server

$area = $_GET['area'];
$SQL = "Read_Location_Area";
$result = sqlsrv_query($conn, $SQL);

and this is the script for SQL server procedure

USE [DP_Rozakana]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Read_Location_Area]
as
BEGIN
select * from Locations where Areas = '$area'
END

finally the sql response for searching data from "Locations" table where Areas like $area(string), not a value from PHP

5

1 Answer 1

0

First, you should check official SQLSRV Driver API Reference, you can find all answers about sqlsrv library.

Now answering your question,

Your $SQL string should be like "{CALL [dbo].[Read_Location_Area]}".

However, you will pass a parameter to the procedure, so your procedure should be:

USE [DP_Rozakana]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Read_Location_Area]
as
    @area NVARCHAR(MAX) -- type of Areas column
BEGIN
    select *
    from   Locations
    where  Areas = @area
END

Then in php, your code eventually looks like:

$area = $_GET['area'];
$params = array(
    array($area, SQLSRV_PARAM_IN)
);
$SQL = "{CALL [dbo].[Read_Location_Area](?)}";
$result = sqlsrv_query($conn, $SQL, $params);

After these steps you should be ready to fetch results.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.