Jump to content

PHP and MS-SQL Stored Procedures


joelhop

Recommended Posts

How do you pass a variable value into an MS-SQL stored procedure in PHP?

I can pass two hardcoded values and it works fine:

<?php


require_once 'Database.php';
$db = Database::connect('Database_Name');


$db->query('pSomeStoredProcedure 26, 1');

?>

However, when I try a variable = 26 instead of 26.
This doesn't work:

<?php


require_once 'Database.php';
$db = Database::connect('Database_Name');
$variable = 26;

$db->query('pSomeStoredProcedure $variable, 1');

?>

Any ideas on how I can pass $variable instead of hard coded 26 into the procedure?

Thanks,
Karl
Link to comment
https://forums.phpfreaks.com/topic/23772-php-and-ms-sql-stored-procedures/
Share on other sites

You can try something like this.....hoepfully should help you

[code]<?php


require_once 'Database.php';
$db = Database::connect('Database_Name');

$arg1 = 26;
$arg2= 1;
$myquery = "pSomeStoredProcedure ". $arg1.",".$arg2

$db->query($myquery);

?>
[/code]
[quote author=joelhop link=topic=111313.msg451015#msg451015 date=1160673647]
[code]
<?php

require_once 'Database.php';
$db = Database::connect('Database_Name');
$variable = 26;

$db->query('pSomeStoredProcedure $variable, 1');

?>
[/code]
[/quote]

Yup, do this instead:

[code]
<?php

require_once 'Database.php';
$db = Database::connect('Database_Name');
$variable = 26;

$db->query("pSomeStoredProcedure $variable, 1");

?>
[/code]

The only difference is single quotes versus double quotes in the query line. Basically, PHP will not convert variables if the string is enclosed in single quotes, but will for double quotes.
Or you could bind the variable using the mssql stored procedure functions.

[code]
require_once 'Database.php';
$db = Database::connect('Database_Name');
$variable = 26;

$query = mssql_init("spSomeStoredProcedure", $db);
mssql_bind($query, "@some_id", $variable, MSSQL_NUM, FALSE, FALSE, 7);

$result = mssql_execute($query);[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.