这有点与php中的Access DateTime->date有关,但实际上是别的东西。我有一些从SQL服务器获取DateTime对象的php。我使用 echo $mydate->format('Y-m-d H:i:s');
所以它只是一个字符串,或者至少我认为它是。(我什至使用显式类型转换来确保)。这是那个代码,
} 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.
}
当"字符串"从类中传递回时,会出现此问题。
$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.
现在它又回到了日期时间对象。我认为没有任何理由将其转换回来。我只是想知道是否有人至少对为什么会发生这种情况有答案,但解决方案会有所帮助。除了在课堂之外进行转换之外的任何事情。
我找到了问题所在。这与我的想法无关。无论如何,这是解决方案。
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(""", $expl);
$expl = explode("'", $wrkStrg);
$wrkStrg = implode("'", $expl);
$expl = explode("&", $wrkStrg);
$wrkStrg = implode("&", $expl);
$expl = explode("<", $wrkStrg);
$wrkStrg = implode("<", $expl);
$expl = explode(">", $wrkStrg);
$wrkStrg = implode(">", $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));
}
}
所以我只需要在那里添加其他内容并移动这两行。我很尴尬,我没有早点看到。
您可以将对象序列化为字符串并将字符串反序列化为对象
阅读此参考资料
http://php.net/manual/en/language.oop5.serialization.php