Jump to content


Photo

PHP and MS-SQL Stored Procedures


  • Please log in to reply
3 replies to this topic

#1 joelhop

joelhop
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 October 2006 - 05:20 PM

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

#2 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 13 October 2006 - 04:32 PM

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

<?php


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

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

$db->query($myquery);

?>

Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#3 MCP

MCP
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 15 October 2006 - 05:20 AM

<?php

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

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

?>


Yup, do this instead:

<?php

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

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

?>

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.

#4 argoSquirrel

argoSquirrel
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 16 October 2006 - 04:42 PM

Or you could bind the variable using the mssql stored procedure functions.

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);





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users