Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SPROC runs fine in MSSMS but fails in php

Author  Topic 

sulman
Starting Member

20 Posts

Posted - 2007-12-12 : 14:29:20
Hi,

I have a sproc that has been coded for me but I am having a problem. The stored procedure runs fine within MS SQL Server Management studio but when I run it through PHP it fails.

This is the PHP code used to run it:

$myServer = "xxxx";
$myUser = "xxxx";
$myPass = "xxxx";
$myDB = "xxxx";

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer");

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB");

$query="EXEC spAllFixtures";
$result=mssql_query($query);
while($row=mssql_fetch_array($result))
{
echo $row['OrganisationID']."<br/>";
}


And this is the stored procedure:


CREATE Procedure spAllFixtures

As
set nocount on

DECLARE @WhatClub int
DECLARE @NumFound int
DECLARE @SQLcommand varchar(1024)
DECLARE @NoFixtureList varchar(1024)

DECLARE cur_find_fixtures CURSOR LOCAL FAST_FORWARD FOR SELECT ClubID FROM tblClubs

OPEN cur_find_fixtures

FETCH FROM cur_find_fixtures INTO @WhatClub

-- WHILE (@@FETCH_STATUS <> -1)
-- BEGIN
--
EXEC @NumFound =dbo.spNumAvailableFixtures @WhatClub

IF @NumFound = 0
SET @NoFixtureList = COALESCE(@NoFixtureList + ',', '') + CONVERT(varchar(12), @WhatClub)

-- FETCH FROM cur_find_fixtures INTO @WhatClub
-- END

CLOSE cur_find_fixtures
DEALLOCATE cur_find_fixtures

SET @SQLcommand = 'SELECT ClubID, Replace(OrganisationID,'''''''','''') AS OrgName FROM tblClubs '
IF @NoFixtureList <> '' SET @SQLcommand = @SQLcommand + 'WHERE ClubID NOT IN (' + @NoFixtureList + ')'
SET @SQLcommand = @SQLcommand + ' ORDER BY ClubID;'

EXECUTE(@SQLcommand)

return


The PHP spits out this error:
Warning: mssql_query() [function.mssql-query]: message: Incorrect syntax near ','. (severity 15) in c:\Inetpub\wwwroot\xxx\test.php on line 21. This line refers to the $result=mssql_query($query); line within the PHP.

Has anyone seen this before?

Thanks

p.s. I realise that this is an SQL forum and not a PHP forum but I was kinda hoping someone from the SQL community may have seen this problem before.

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2007-12-18 : 16:42:41
COuple things jump out.

YOu have a cursor that isn't exactly "cursoring" because those parts of the procedure are commented out.

Then what is happening is you have a SQL Statement that would have an extra comma in the IN() list for @noFixtureList

SET @NoFixtureList = COALESCE(@NoFixtureList + ',', '') + CONVERT(varchar(12), @WhatClub)


Since you are adding the comma to the @NoFixtureList your IN statement the error is actually a syntax error in the resulting Select statement.

To avoid this make sure you remove that last comma so your IN() criteria (if applicable) doesn't have an extra comma.






Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page
   

- Advertisement -