|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2005-12-29 16:33 UTC] muratyaman at gmail dot com
 Description:
------------
Error message is:
"mssql_query() [function.mssql-query]: Possible network error: Bad token from SQL Server: Datastream processing out of sync. (severity 9)
   in file ... line ..."
I have a WinXP, Apache2, PHP5, MSDE2000 app and many pages inserting, updating, deleting records..
Pages for inserts include code to return on error and not lose entered data.
Reproduce code:
---------------
(Every page opens connection, queries database multiple times, closes the connection.)
Create a form with a few textboxes and a combobox.
I create a combobox with:
<?
$selected_option = 0;
echo db_combo('field1', 'select field1 from tbl1', $selected_option);
?>
db_combo simply uses mssql_query to select records from a table, fill with options, create HTML code of a combobox with
<select>
  <option></option>
</select>
I use a global $dbh to handle database connection, also use  mssql_connect, mssql_select_db functions.
Expected result:
----------------
1. load page: tbl2_new.php 
   combobox FULL with records read from the table
2. post to  : tbl2_insert.php
   may not insert record because of normal reasons
3. return to: tbl2_new.php
   any entered data is post back
   combobox FULL with records read from the table
4. post to  : tbl2_insert.php
   insert record
Actual result:
--------------
1. load page: tbl2_new.php 
   with EMPTY combobox and ERROR
   connection is lost
   every call afterwards to mssql_query fails
2. post to  : tbl2_insert.php
   may or may not insert record
3. return to: tbl2_new.php
   any entered data is post back
   combobox FULL with records read from the table
   no problem at all
Briefly, "new" and "edit" pages are very much similar with comboboxes etc. But on "new" pages, combobox cannot get any records on 1st run.
I could not figure out the problem.
But I think there is a communication problem when SQL server tries to send the records from a table.
Thanks in advance.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 17:00:02 2025 UTC | 
Thank you. I could not integrate php_dblib.dll :( I tried to compile FreeTDS using Dev-C++ but could not get it working with PHP.. I am stuck. I was investigating the problem. I have a function as follows: function db_get_next_id($mygenid=''){ $i=0; $sql=" DECLARE @MY_ID INT8, @r int8 EXECUTE @r= SPMY_GET_NEXT_ID \"$mygenid\", @MY_ID OUTPUT " ; if($qry=db_query($sql)){ while($row=db_fetch_row($qry)){ $i=$row[0]; break; } db_free_result($qry); } return $i; }//end fun get next id $new_id=db_get_next_id('keyfield'); This just works fine, I found that after this, calls to the db fails and produces the error. Because when I comment out the line calling this function, I don't get any error message. Any ideas? RegardsBasically, my procedure inserts a dummy record into a special table and gets inserted id. (int8 is a udt for 'bigint', i'm changing it for you below to bigint). I have dummy tables to simulate sequence generators for different tables, here is a pair of them: CREATE TABLE TBL_GEN_ABC_ID ( ID bigint IDENTITY(1, 1) NOT NULL, DUMMY bit, CONSTRAINT PK_TBL_GEN_ABC_ID PRIMARY KEY CLUSTERED (ID) ) GO CREATE TABLE TBL_ABC ( ABC_ID INT NOT NULL, ABC_NAME] VARCHAR(100) NOT NULL, CONSTRAINT PK_TBL_ABC PRIMARY KEY CLUSTERED (ABC_ID) ) GO Procedure is like this: CREATE PROCEDURE spmy_get_next_id( @GEN_ID_NAME VARCHAR(100)='#NO TABLE', @ID bigint OUTPUT ) AS BEGIN SET @ID=NULL; DECLARE @GEN_ID bigint; SET @GEN_ID=0; IF (UPPER(@GEN_ID_NAME)='ABC_ID') BEGIN WHILE (1=1) BEGIN --generate id INSERT INTO TBL_GEN_ABC_ID (DUMMY) VALUES(0); SET @GEN_ID=@@IDENTITY; --get generated id IF(NOT EXISTS( --make sure it was not used SELECT ABC_ID FROM TBL_ABC WHERE ABC_ID=@GEN_ID) ) BREAK; END END SET @ID=@GEN_ID; SELECT @ID AS ID;--for PHP to read resultset and value RETURN @ID; --return value, not crucial END GO so in PHP you can use modifed version of my function: function db_get_next_id($mygenid=''){ $i=0; $sql=" DECLARE @MY_ID bigint, @r bigint EXECUTE @r=SPMY_GET_NEXT_ID \"$mygenid\", @MY_ID OUTPUT " ; if($qry=db_query($sql)){ while($row=db_fetch_row($qry)){ $i=$row[0]; break; } db_free_result($qry); } return $i; }//end fun get next id $new_id=db_get_next_id('ABC_ID'); (As I mentioned earlier, e.g. db_fetch_row is just using mssql_fetch_row.. all of my db_xyz functions are like this.) This works fine but subsequent mssql_query functions fail. This may not be the ideal way of doing it, but it should not cause any harm. Everything else is working fine. Maybe I should just change the way I use my procedures.. Thank you.Hi again :) I changed my function to this: function db_get_next_id($mygenid=''){ $id = 0; $ret_val=0; $stmt = mssql_init("SPMY_GET_NEXT_ID"); mssql_bind($stmt, "@GEN_ID_NAME", &$mygenid, SQLVARCHAR, FALSE,FALSE);//input, not null mssql_bind($stmt, "@ID", &$id, SQLINT4, TRUE,TRUE);//output,null mssql_bind($stmt,"RETVAL",&$ret_val,SQLINT4); $result = mssql_execute($stmt); while($row=mssql_fetch_row($result)){ //read id $id=$row[0];//but it should get from the output variable!? } mssql_free_statement($stmt); unset($stmt); // <---VERY important return $id; } Anyway, this solved my problem. But will I not be able to use arbitrary SQL statements with mssql_query, including execution of procedures, etc. ?! Kind regards