joelhop Posted October 12, 2006 Share Posted October 12, 2006 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:<?phprequire_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:<?phprequire_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 More sharing options...
shivabharat Posted October 13, 2006 Share Posted October 13, 2006 You can try something like this.....hoepfully should help you[code]<?phprequire_once 'Database.php';$db = Database::connect('Database_Name');$arg1 = 26;$arg2= 1;$myquery = "pSomeStoredProcedure ". $arg1.",".$arg2$db->query($myquery);?>[/code] Link to comment https://forums.phpfreaks.com/topic/23772-php-and-ms-sql-stored-procedures/#findComment-108429 Share on other sites More sharing options...
MCP Posted October 15, 2006 Share Posted October 15, 2006 [quote author=joelhop link=topic=111313.msg451015#msg451015 date=1160673647][code]<?phprequire_once 'Database.php';$db = Database::connect('Database_Name');$variable = 26;$db->query('pSomeStoredProcedure $variable, 1');?>[/code][/quote]Yup, do this instead:[code]<?phprequire_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. Link to comment https://forums.phpfreaks.com/topic/23772-php-and-ms-sql-stored-procedures/#findComment-108963 Share on other sites More sharing options...
argoSquirrel Posted October 16, 2006 Share Posted October 16, 2006 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] Link to comment https://forums.phpfreaks.com/topic/23772-php-and-ms-sql-stored-procedures/#findComment-109609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.