1

This kinda relates to Access DateTime->date in php but is actually something else. I have some php that gets a DateTime object from a SQL server. I Convert it using echo $mydate->format('Y-m-d H:i:s'); so that its just a string or at least i think it is. (I even used explicit type casting to make sure). here is that code,

                } else {
                echo "<br/>object ";
                $dt = (string) $row[$column]->format('Y-m-d H:i:s');
                $this->retVal["nonEmpty"] = true;
                $this->retVal["value"] = (string) $dt;
                echo $this->retVal["value"]; //Returns a string, no issue.
                }

The problem occurs when the "string" is passed back out of the class.

                $convert = new DataConv;
                $convert->conv($newConnectionThree, $fieldNames[$cnt1][$cnt2]["Type"],$fieldNames[$cnt1][$cnt2]["Size"], $tableNames[$cnt1],              $columnNames[$cnt1][$cnt2]);
                $fieldNames[$cnt1][$cnt2]["Type"] = $convert->type;
                $returned = $convert->retVal;
                echo $returned["value"]; //Returns error "Catchable fatal error: Object of class DateTime could not be converted to string".
                var_dump($returned["value"]); //Shows Datetime object. 

Now its back to being a Datetime object. I don't see any reason for it to be converted back. I was just wondering if anyone has an answer for at least why this happens, but a solution would be helpful. Anything short of making that conversion outside of the class.

Community
  • 1
  • 1
Matthew Goheen
  • 819
  • 8
  • 12
  • Can you show the complete code for your DataConv class? – vascowhite Jul 12 '16 at 08:22
  • The whole class is 209 lines. Basically what it does is takes the numerical value for data type that's returned from SQL in META DATA and converts it to its string value(a type of "-9" would now be stored as "N VarChar") and stores it in a variable that can be accessed outside of the class, then fetch the first row of that table and store the value in an array that can be accessed outside the class. – Matthew Goheen Jul 12 '16 at 16:22

2 Answers2

0

I found what the issue was. It had nothing to do with what I was thinking. Anyway, here is the solution.

private function get($conn, $table, $column){
    $params = array();
    $expl = array();
    $wrkStrg;
    $tsql = "SELECT [" . $column . "] FROM " . $table;
    $options = array("scrollable" => SQLSRV_CURSOR_KEYSET);
    $getTableData = SQLSRV_QUERY($conn, $tsql, $params, $options);
    if ($getTableData){
    $row = SQLSRV_FETCH_ARRAY($getTableData, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_FIRST);
        if ($row !== NULL){
            if (!is_object($row[$column])){
                if (!preg_match('/[\"\'\&\<\>]/', $row[$column])){
                    if (($this->type === "Image") or ($this->type === "Var Binary")){
                        $this->retVal["nonEmpty"] = true;
                        $this->retVal["value"] = bin2hex($row[$column]);
                    } else {
                        $this->retVal["nonEmpty"] = true;
                        $this->retVal["value"] = $row[$column]; //These two lines. 
                    }
                } else {
                    $expl = explode("\"", $row[$column]);
                    $wrkStrg = implode("&quot;", $expl);
                    $expl = explode("'", $wrkStrg);
                    $wrkStrg = implode("&#39;", $expl);
                    $expl = explode("&", $wrkStrg);
                    $wrkStrg = implode("&amp;", $expl);
                    $expl = explode("<", $wrkStrg);
                    $wrkStrg = implode("&lt;", $expl);
                    $expl = explode(">", $wrkStrg);
                    $wrkStrg = implode("&gt;", $expl);
                    $this->retVal["nonEmpty"] = true;
                    $this->retVal["value"] = $wrkStrg;
                    $expl = array();
                    $wrkStrg = Null;
                }
            } else {
                $this->retVal["nonEmpty"] = true;
                $this->retVal["value"] = $row[$column]->format('Y-m-d H:i:s');
            }
            //Used to be here! :(
        }
    } else {
        echo "<p>Table: " . $table . "<br/>Column: " . $column . "</p>";        
        echo "<p>Falure: Seven</p>";
        die(var_dump(SQLSRV_ERRORS(), true));
    }
}

So I just needed to add that else in there and move those two lines. I'm embarrassed that I didn't see that sooner.

Matthew Goheen
  • 819
  • 8
  • 12
-1

you can serialized an object to string and unserialize string to object
read this reference
http://php.net/manual/en/language.oop5.serialization.php

Fadakar
  • 509
  • 1
  • 5
  • 21