views:

284

answers:

1

Hi, I don't understand why this piece of code miserably fails. The first pass of the for() loop works fine but in the second pass mssql_query() fails, no errors are reported and the program just die.

for($i=0; $i <2; $i++){
       $query = "SELECT * FROM Viaggio ";
       $result = mssql_query($query, $link) or die("query fallita:".msql_error());
          if( mssql_num_rows($result) ){
             while($row = mssql_fetch_array($result, MSSQL_ASSOC)){
                echo 'blablablabla' . $row[some_index] . 'blabla';
             }
          }
    }

I don't know why this happen, It's works with all the other tables in my database except for Viaggio. This is the CREATE TABLE query directly from msSQL:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Viaggio](
 [codViaggio] [bigint] IDENTITY(0,1) NOT NULL,
 [data] [smalldatetime] NOT NULL,
 [oraArrivo]  AS ([data]+[durata]),
 [corsaExpress] [bit] NOT NULL,
 [durata] [time](7) NOT NULL,
 [fascia] [nvarchar](50) NOT NULL,
 [distanza] [char](3) NOT NULL,
 [nave] [nvarchar](50) NOT NULL,
 [partenza] [nvarchar](50) NOT NULL,
 [arrivo] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Viaggio] PRIMARY KEY CLUSTERED 
(
 [codViaggio] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Viaggio]  WITH CHECK ADD  CONSTRAINT [FK_Viaggio_Nave] FOREIGN KEY([nave])
REFERENCES [dbo].[Nave] ([nome])
ON UPDATE CASCADE
GO

ALTER TABLE [dbo].[Viaggio] CHECK CONSTRAINT [FK_Viaggio_Nave]
GO

ALTER TABLE [dbo].[Viaggio]  WITH CHECK ADD  CONSTRAINT [FK_Viaggio_Rotta] FOREIGN KEY([partenza], [arrivo])
REFERENCES [dbo].[Rotta] ([pPartenza], [pArrivo])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[Viaggio] CHECK CONSTRAINT [FK_Viaggio_Rotta]
GO

ALTER TABLE [dbo].[Viaggio]  WITH NOCHECK ADD  CONSTRAINT [FK_Viaggio_Tariffa] FOREIGN KEY([fascia], [distanza])
REFERENCES [dbo].[Tariffa] ([fascia], [distanza])
ON UPDATE CASCADE
GO

ALTER TABLE [dbo].[Viaggio] CHECK CONSTRAINT [FK_Viaggio_Tariffa]
GO

Thank you very much

[EDIT] I just tried to use mssql_free_result() but also this function crash the program.

+1  A: 

You are using the mssql library for each db call, but the msql library to fetch the error - is this correct?

adam
I don't know if this is correct (I think no) but if i remove the "or die..." option nothing changes.
Federico
Why are you looping through the code twice? Try outputting mssql_num_rows($result)
adam
The first time mssql_num_rows return 12 (the correct number of records in the table Viaggio) the second time the program hangs on mssql_query(). Nothing is returned from mssql_query() calls and the program dies.
Federico
It sounds like the mssql server is tripping out or taking too long to reply. You could try increasing the php script time limit with set_time_limit. Again, if you're looping through the same query twice then why not just query once and reuse the initial results?
adam
Ok thank you very much, the problem was the mssql databese we HAVE to use is in a very crappy server. Adjusting the set_time_limit works well!Obviously I need to looping through the same query but with different condition each time, I just simplified the query for posting :)
Federico
False alarm the problem is still present. :(
Federico
Either it works or it doesn't! What about if you remove the time limit completely with set_time_limit(0)?
adam
no difference with set_time_limit(0). The problem is if I do query that doesn't return any records for example "SELECT * in Viaggio WHERE date = "date_not_present"" it works. But if the query return something I can access the data normally but if I call again mssql_query() the program suddenly stops.
Federico