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
Share on other sites

[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.
Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.