PHP adodb COM recordset returning variant:__set_state for dates/times, how can I just get them as strings?

StackOverflow https://stackoverflow.com/questions/17794936

  •  03-06-2022
  •  | 
  •  

سؤال

I'm accessing an mssql db in PHP and creating the connection as a new COM() object. My query runs perfectly fine. When I do the query in Microsoft SQL Server 2008, everything looks great, the dates/times show up in columns and are readable.

But when I run the same query and return the results in PHP, I go to iterate through the results like so:

$selected_fields = array("FS.fund_name", "M.media_id", "MT.media_type_name", "modified_date", "file_upload_date", "issued_date", "filename");
//query goes here
$record_set = new COM("ADODB.Recordset");
while(!$record_set->EOF){ //loop through query results and add to inline cache variable
  $temp_record = array();
  foreach($selected_fields as $fieldname){ //go through fields and add to array
      $fieldname_array = explode(".", $fieldname); //separate table and column
      $fieldname = $fieldname_array[sizeof($fieldname_array)-1];
      $temp_record[$fieldname] = $record_set->fields[$fieldname]->value;
  }
  print_r($temp_record);
  $rs_array[] = $temp_record;
  $record_set->movenext();
}

As I print each $temp_record, they end up looking like this:

Array ( [fund_name] => TEST [media_id] => 1001 [media_type_name] => 8937 Tax Forms [modified_date] => variant Object [file_upload_date] => variant Object [issued_date] => variant Object [filename] => form8937test.pdf )

So the dates are some kind of object, and I don't know how to access them in PHP. And when I go to save them as a cache file, they have this value:

variant::__set_state(array())

How can I retrive the date/time in a format that I can then manipulate using PHP? I want to filter them by year, and sort them by most recent.

هل كانت مفيدة؟

المحلول

According to Variant Object documentation, you should be able to access it by

$temp_record['modified_date']->value; 

or

$val = (string)$temp_record['modified_date']; 

From the same doc:

the variant is converted to a PHP value only when there is a direct mapping between the types that would not result in a loss of information. In all other cases, the result is returned as an instance of the VARIANT class.

So to check if cast is needed, simply check if you have an object of Variant class:

if (is_object($var) && $var instanceof Variant) {
    $val = (string)$var;
} else {
    $val = $var;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top