পৃষ্ঠাসমূহ

মঙ্গলবার, ২ নভেম্বর, ২০১০

How to call MSSQL Stroed Procedure using PHP

Before beginning my writing, let me tell you about the errors generally an programmer can face. The errors may be like this:
mssql_bind() : unsupported type in line #
mssql_execute() : procedure needs parameter which is not supplie

Here is the solution:
First we have to know the general declaration of mssql_bind() function and its parameter. It is used to call a stored procedure of SQL Server 2000 using PHP.

mssql_bind(resource $string, "@Formal parameter", &$variable, int $TYPE, "bool is_output", "bool is_null", int maxlength);

Explanation of the parameters.
1. resource $string: This string can be obtained from mssql_init()

2. @Formal parameter: This is the name of formal parameter which is used in stored procedure.
You must need to add '@' character before parameter name.

3. &$variable: The PHP variable which you are using to bind to MSSQL parameter.

4. $TYPE: There are different types, such as: SQLTEXT, SQLVARCHAR, SQLCHAR, SQLINT1, SQLINT2, SQLINT4, SQLBIT, SQLFLT4, SQLFLT8, SQLFLTN. It depends what type of parameter you used in the stored procedure.

5. bool is_output: Whether the parameter of stored procedure is declared as Output ot not. If it is declared as output then value will be TRUE. Otherwise false. If you mention nothing then program will consider it as FALSE. Error message will be generated if you don't mention it correctly.

6. bool is_null: Whether the parameter of stored procedure is null or not. If it is null then we have to write TRUE otherwise FALSE.

7. int maxlength: The maximum length of parameter.

A simple example is given:

// Connect to MSSQL and select the database
mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php');

// Create a new stored prodecure
$stmt = mssql_init('NewUserRecord');

// Bind the field names
mssql_bind($stmt, '@username', 'Kalle', SQLVARCHAR, false, false, 60);
mssql_bind($stmt, '@name', 'Kalle', SQLVARCHAR, false, false, 60);
mssql_bind($stmt, '@age', 19, SQLINT1, false, false, 3);

// Execute
mssql_execute($stmt);

// Free statement
mssql_free_statement($stmt);


An important thing is to remember:
Sometimes we write the code correctly but it shows error. In this case we have to check the "php.ini" file. You have to turn "allow_call_time_pass_reference" to ON. To do this, go to php.ini file and find it. Then uncomment it(remove the semi colon) and write "ON" on its write side.

Reference:
http://php.net/manual/en/function.mssql-bind.php
http://theserverpages.com/php/manual/en/function.mssql-bind.php
http://www.phptutorial.info/?mssql-bind




কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন