Problem populating PHP array from C extension

[email protected] (Rich Bryant) Sun, 23 Jan 2000 20:43:11 -0500
Newsgroups php.beta
Message-ID <[email protected]>
I'm having great difficulty in loading a PHP array from within my own
C module, and thought someone here might be able to provide help.
The code in question appears below.  Here's a quick intro to the code
segment - any observations would be most appreciated.

The main function is 'cursor_init', which starts at line 8.  It
references the callback function 'PageAppend' (defined at line 2) to
populate the array via calls to 'add_next_index_long'.

Note that, because the PageAppend callback receives only a single long
as it's input parameter the function first sets the global static
pval pointer 'pagearray' to point to the PHP array variable (line 17).

One thing I noted is that I'm not using a 'convert_to...' on the array
variable when I enter the function because I can find no description
of a 'convert_to_array' function call.  Could this be a problem???

The real action happens on line 23 where I call 'CursorInit' (an external
 C function) with 'PageAppend' designated as the callback function.
When I comment out line 3 to populate the array everything runs ok (of
course the array isn't populated), however when I uncomment this line
the httpd request completely bombs out.

Any help would be greatly appreciated!

-Rich Bryant


=== Code Appears Below Here ===

 1	static pval *pagearray;
   
 2	static void PageAppend(long l1) {
 3	add_next_index_long(pagearray,l1);
 4	}
   
 5	/*
 6	  proto cursor_init();
 7	*/
 8	PHP_FUNCTION(cursor_init) {
 9	  pval *db, *stmt, *nRecs, *Sig, *TotalRecs, *PageArray;
10	  char *errMsg, *new;
11	  char signature[60];
12	  long l1, totalrecs;
   
13	  if(ARG_COUNT(ht) != 6 || getParameters(ht, 6, &db,&stmt,&nRecs,&Sig,&TotalRecs,&PageArray) == FAILURE) {
14	    WRONG_PARAM_COUNT;
15	  }
   
16	  // Note: TotalRecs, Sig and PageArray must be passed by reference.
17	  pagearray = PageArray;     // Est variable for callback function.
18	  convert_to_string(db);
19	  convert_to_string(stmt);
20	  convert_to_string(Sig);
21	  convert_to_long(nRecs);
22	  convert_to_long(TotalRecs);
   
23	  errMsg = CursorInit(db->value.str.val,stmt->value.str.val,nRecs->value.lval,signature, 60, &totalrecs, PageAppend);
   
24	if (errMsg==NULL) {
25	  // Here set values returned by reference.
26	  Sig->value.str.val = estrdup(signature);
27	  TotalRecs->value.lval = totalrecs;
28	  errMsg = "";
29	  }
30	l1 = (long)strlen(errMsg);
31	new = estrdup(errMsg);
32	RETURN_STRINGL(new,l1,0);
33	}