i've an $object coming out from a mysql_fetch_object.

SQL ok, result ok..: everything okay when print_r($obj).

next, I loop through $obj because I need to rework some fields (cut date, strings, so on)

If I comment one (RANDOMLY!!!!) line where I do a substr() the code work... commented out, the server doesn't return anything

it's a memory leak? error in my code? I'm going crazy....

this is the code snippet:

while($obj = mysql_fetch_object($result)) {

/ convert obj to array... /
$obj_array[$rowIndex] = get_object_vars($obj);

/ just print out first record only for debug /
if($rowIndex == 0) {
echo "<pre>";
var_dump($obj);
echo "</pre>";
}
/ this doesn't work too!! /
//$obj_array[$rowIndex] = (array)$obj;

/ this doesn't work too!!
$tmp = (string)$obj_array[$rowIndex][paz_Sex];
$tmp2 = substr($tmp,0,1);
$obj_array[$rowIndex][paz_Sex] = $tmp2;
/

    $obj_array[$rowIndex][pr_Data_Pren] = substr($obj->pr_Data_Pren,0,10);
    $obj_array[$rowIndex][pr_Ora_Pren] = substr($obj->pr_Ora_Pren,11,5);
    $obj_array[$rowIndex][pr_Data_Reg] = substr($obj->pr_Data_Reg,0,10);

    $obj_array[$rowIndex][paz_Com_res] = "?".$obj->paz_Com_res;
    $obj_array[$rowIndex][paz_Com_nasc] = "?".$obj->paz_Com_nasc;
    $obj_array[$rowIndex][pr_Prest_ICD9] = "?".$obj->pr_Prest_ICD9;


    $rowIndex++;

}

    Memory leak doubtful. What you are doing isn't new to anyone here. However, I think this would be better fit in the newbies section. Or even databases. Doesn't matter though,

    What you should read up on is the PHP manual. There is a function there named. mysql_fetch_array()

    Which does the same thing you are trying to do. As far as I know everyone uses it. A snippet of my code would look something like this:

    // Make sure your mysql connections are right.
    
    // Here is your SQL statement to run on
    // mysql.
    $sql_statement = "SELECT field1,field2 FROM tblname";
    
    // Obtain a Result_ID that will be passed by
    // mysql when running a mysql_query() function
    $sql_result = mysql_query($sql_statement);
    
    // Take the Result_ID returned by mysql and 
    // grab the data that was passed by mysql
    // in the form of an array with the mysql_fetch_array() function
    while($sql_array = mysql_fetch_array($sql_result))
    {
         // Get values from the array passed by mysql
         // Display the values as well.
         $field1 = $sql_array['field1'];
         $field2 = $sql_array['field2'];
         echo "$field1 :: $field2";
    }
    // Close your database connection!
    mysql_close($db);
    ?>
    

    That is all you have to do.

    There are many other ways of doing this. But this one works for me and haven't had any problems with it ever. Also this style can be taken into any other database since mysql functions are the same as MSSQL, MSQL, and many others in PHP.

    Lata,

    Chad

    EDIT: Forgot to end my PHP block. :-D

      dear chads2k2,

      I've posted here becuase:

      • the question is not database specific: the question is: why this snippet doesn't work? what's the reason of this malfunction, whitout any sort of error,warning,notice from PHP and with a so strange behaviour?
        IMHO it doesn't matter if this is the "better" way to do this: why does php guys developed a mysql_fetch_object func? I think it's not only to allow some rtfm in some post 🙂

      • What if my data where coming not from a DB but stored into an $obj instead?

      I this could be an " academic post " without a pratical solution totally reworking the code, but I want also to know if someone out there could explain me whay, substr() in conjunction with array assignement bring me to this crappy beheaviour

      thank you anyway

      (ps. I think it isn't a newbee question, isn't it?)

        Originally posted by pivan
        IMHO it doesn't matter if this is the "better" way to do this: why does php guys developed a mysql_fetch_object func? I think it's not only to allow some rtfm in some post 🙂

        Just because it's there it doesn't mean you have to use it. Unless you want to claim "I do Object Oriented Programming! points"....🙂

        - What if my data where coming not from a DB but stored into an $obj instead?

        Have you tried this scenario to see if the error still occurs? Get all the irrelevant (as you claim) database-specific stuff out of the way and focus on "using substr in conjunction with array assignment" in other words (which I notice doesn't mention objects either).

        Also, there's not a lot of point extracting all those object fields into an array if you're just going to overwrite the elements of that array with the contents of the fields again. (Since you say that the database isn't important, I'll not go into its apparent ill-design.)

        $rowIndex = 0;
        while($obj = mysql_fetch_object($result))
        {
        	$temp_array = get_object_vars($obj);
        
        if($rowIndex == 0)
        {
        	echo "<pre>";
        	var_dump($obj);
        	echo "</pre>";
        }
        
        $temp_array['paz_Sex']       = substr((string)$temp_array['paz_Sex'],0,1);
        $temp_array['pr_Data_Pren']  = substr($temp_array['pr_Data_Pren'],0,10);
        $temp_array['pr_Ora_Pren']   = substr($temp_array['pr_Ora_Pren'],11,5);
        $temp_array['pr_Data_Reg']   = substr($temp_array['pr_Data_Reg'],0,10);
        $temp_array['paz_Com_res']   = "?".$temp_array['paz_Com_res'];
        $temp_array['paz_Com_nasc']  = "?".$temp_array['paz_Com_nasc'];
        $temp_array['pr_Prest_ICD9'] = "?".$temp_array['pr_Prest_ICD9'];
        
        // Do a bit of debugging
        if($rowIndex == 0)
        {
        	echo "<pre>";
        	var_dump($temp_array);
        	echo "</pre>";
        }
        
        $obj_array[$rowIndex] = $temp_array;
        $rowIndex++;
        }
        //Oh, what the hey:
        echo "<pre>";
        var_dump($obj_array);
        echo "</pre>";
        

        Since no-one has access to your data or how it's laid out, nor the results of your debugging, and since (as chads2k2 has pointed out), hordes of people work with array assignments and substr() without any ill effect, all anyone can do is continue to suggest that maybe the problem lies in the bits you haven't shown us.

        And lastly; don't go proposing "memory leak?" - especially if you don't know what it means - if your diagnostic skills were that great you wouldn't need to be asking the question, would you? Why would you think it was a memory leak? After all, if it's not, your post's subject line is less than useless.

          ok...just to avoid any doubt, this is the version with array fetch instead of object fetch. Can you explain the behaviour of the script?

          With the comment it work, wothout..not. Crazy, isnt'it?

          $rowIndex = 0;
          $colNames = array();
          
          while($arr = mysql_fetch_assoc($result)) {
          
              if($rowIndex == 0) {
                $ind = 0;
                while (list($key, $value) = each($arr)) {
                   $colNames[$ind] = $key;
                   $ind ++;
                }
              }
          
              // label with "?" numeric field that should be treated as strings
          
              $obj_arr[$rowIndex] = $arr;
          
              $obj_arr[$rowIndex][pr_Data_Pren] = substr($arr[pr_Data_Pren],0,10);
              $obj_arr[$rowIndex][pr_Ora_Pren] = substr($arr[pr_Ora_Pren],11,5);
              $obj_arr[$rowIndex][pr_Data_Reg] = substr($arr[pr_Data_Reg],0,10);
          
              if($tabella == 'storico') {
                      $obj_arr[$rowIndex][pr_Data_Chius] = substr($arr[pr_Data_Chius],0,10);
              }
          
          //      $obj_arr[$rowIndex][paz_Sex] = substr($arr[paz_Sex],0,1);
                  $obj_arr[$rowIndex][paz_Com_res] = "?".$arr[paz_Com_res];
                  $obj_arr[$rowIndex][paz_Com_nasc] = "?".$arr[paz_Com_nasc];
                  $obj_arr[$rowIndex][pr_Prest_ICD9] = "?".$arr[pr_Prest_ICD9];
          
              $rowIndex++;
          }
          

            data into the DB is quite common, and DB architecture nothing sci.fi. Simply, I need to rework some fileds just to prepare them for PEAR:Spreadsheet_excel_writer.
            If you put a "0064" (which is an example of a code) into an Excel field, without telling it's a string it will print 0064. And because of the data I have, 0064 != 64 :-)

            This is debugging info. Offcourse with one of the assignment I do commented out..unless it doesn't work

            Have you ever programmend in C? Sometime, becouse of a mistake, it's quite a printf() that can avoid a seg. fault. This is the main reason of my topic subjec. The second is that, becouse of my experience, when programs doesn't put anything out (no output, no error) this could be related to a problem in memory assignement not jet detected during script parsing.

            Every field is recognized as string. So maybe ther's not a need of cast.

            I'm here to learn, not to raise universe entropy! I wasn't sure of a bug in PHP function substr or in array management (but quite sure there's a bug in my code) unless I would have had posted to PHP bugtraq ;-)

            BEFORE PROCESSING:array(15) {
              ["paz_ID"]=>
              string(2) "37"
              ["paz_Sex"]=>
              string(7) "FEMMINE"
              ["paz_Nato"]=>
              string(10) "1930-12-17"
              ["paz_Com_nasc"]=>
              string(6) "010025"
              ["paz_Com_res"]=>
              string(6) "010032"
              ["pr_ID"]=>
              string(2) "37"
              ["pr_Classe"]=>
              string(2) "A2"
              ["pr_APP"]=>
              string(7) "1216.67"
              ["pr_Data_Pren"]=>
              string(19) "2003-12-18 16:02:00"
              ["pr_Ora_Pren"]=>
              string(19) "2003-12-18 16:02:00"
              ["pr_Data_Reg"]=>
              string(19) "2003-02-18 00:00:00"
              ["pr_Data_Chius"]=>
              string(19) "2004-02-18 00:00:00"
              ["pr_Esito"]=>
              string(8) "RICOVERO"
              ["pr_Prest_ICD9"]=>
              string(3) "064"
              ["pr_Prest_ICD9_Descrizione"]=>
              string(22) "Tiroidectomia completa"
            }
            
            AFTER PROCESSING:array(15) {
              ["paz_ID"]=>
              string(2) "37"
              ["paz_Sex"]=>
              string(1) "F"
              ["paz_Nato"]=>
              string(10) "1930-12-17"
              ["paz_Com_nasc"]=>
              string(7) "?010025"
              ["paz_Com_res"]=>
              string(7) "?010032"
              ["pr_ID"]=>
              string(2) "37"
              ["pr_Classe"]=>
              string(2) "A2"
              ["pr_APP"]=>
              string(7) "1216.67"
              ["pr_Data_Pren"]=>
              string(10) "2003-12-18"
              ["pr_Ora_Pren"]=>
              string(19) "2003-12-18 16:02:00"
              ["pr_Data_Reg"]=>
              string(10) "2003-02-18"
              ["pr_Data_Chius"]=>
              string(10) "2004-02-18"
              ["pr_Esito"]=>
              string(8) "RICOVERO"
              ["pr_Prest_ICD9"]=>
              string(4) "?064"
              ["pr_Prest_ICD9_Descrizione"]=>
              string(22) "Tiroidectomia completa"
            }
            
            

              Well, it sounds like you got it fixed - and the devil was in the absent details.

              Originally posted by pivan
              Have you ever programmend in C? Sometime, becouse of a mistake, it's quite a printf() that can avoid a seg. fault. This is the main reason of my topic subjec. The second is that, becouse of my experience, when programs doesn't put anything out (no output, no error) this could be related to a problem in memory assignement not jet detected during script parsing.

              As you've discovered, PHP is not C. Any "memory assignment" error would be a fault in the PHP interpreter, and not in the code (and hence, once isolated, a subject for the PHP bug list). But "memory leak" isn't synonymous.

                Thank you for the link about the memory leak. I'will adopt terms like "memory assignment" or "memory allocation" in the future, more appropriate for the concept I was trying to communicate (not enough memory, wrong memory wiritings.. and so one)

                I'm trying to isolate the problem, unsuccessfully at the moment. It seems to work with a simple array as input. I'm thinking also to fetch data and write it to excel stream into the same loop, maybe it's a better tecnique, isn't it?

                thank you

                ivan

                  I've found that printing all of the resulting dataset (reworked) some fields of the last item of my arrayOfArray where incomplete.

                  Printing only the last item, or commenting out one of the processing I did on the single string elements, is ok.

                  I've arrayOfArray, 140 records, each with an array of about 15 records inside and an amount of 100 to 130 characters (sum of all fields in the 15 record array)

                  this bring the "raw" size of the structure to about 200K .....I don't know if I'm speaking correctly but could be this a PHP limit?

                  thank you?

                    Write a Reply...