问题
We have a uni student doing work experience at the moment and he's doing a Wordpress prototype for us (as we have neither Wordpress or PHP experience).
It's running on a Windows server and while Wordpress itself is running in mySQL, as all our existing databases are in MS SQL Server 2005/2008 and he's trying to call a stored procedure in a php page using this code:
$connection = odbc_connect('DB', 'UNAME', 'PWORD');
$request = odbc_prepare($connection, "CALL ProcName(?, ?, ?)");
if(!$request) die("Could not prepare statement:" . odbc_errormsg());
$result = odbc_execute($request, array("var1", "var2", "var3"));
if(!$result) die("Could not execute statement:" . odbc_errormsg());
The stored procedure is like this:
ROCEDURE [dbo].[ProcName]
(@option1 varchar(50),
@option2 varchar(50),
@option3 varchar(50))
AS
... lots of logic end with...
select * from tblName
The stored procedure is used both by .net pages and Livelink CMS pages and works correctly but when we try to call it from php, it errors with:
"odbc_execute(): SQL error: [Microsoft][ODBC SQL Server Driver]Invalid parameter number, SQL state S1093 in SQLDescribeParameter in C:\inetpub\wordpress\test.php on line 29"
Strangely, if we rename the procedure call to a non existant stored procedure, it errors with exactly the same thing rather than a stored procedure cannot be found type of error.
We can run sql directly i.e. "select * from etc" and it will return data but we can't call stored procedures (which we use for everything of course!).
Any idea where he could be going wrong?
回答1:
As I can see from the error message, the error should come from the ODBC driver, because from it's perspective the parameter number differs from prepare to execute statements.
Ask your student to try this approach:
$parms=array("var1", "var2", "var3");
if (!odbc_execute($request, &$parms)) die("odbc_execute failed");
回答2:
I got the same error when I was doing an native SQL query from Java EE/Hibernate. I was querying "SELECT Count (1) From Table" and I made the mistake of including a mapping class as a parameter. COUNT(1) returns an integer, so it doesn't need to be mapped. Hope that helps.
来源:https://stackoverflow.com/questions/17918459/invalid-parameter-number-sql-state-s1093-php-odbc-mssql