[英]Switch from mySQL to mariaDB timestamp messup
我已經從MySQL切換到MariaDB,這引起了一些“較小”的問題。 一個小時來我一直在煩我,而我找不到解決方案。
我通過從MySQL導出數據庫並將其導入到MariaDB進行了遷移,從而移動了數據庫。
當我的更新查詢之一不起作用時,我將其縮小到數據庫處理程序中的此函數:
public function updateEquipment($type,$product,$acquisition,$calibration_interval,$equipment_no,$inspection_date,$equipment_id,$active)
{
$stmt = $this->conn->prepare("UPDATE equipment SET type = :type, acquisition = :acquisition, calibration_interval = :calibration_interval, equipment_no = :equipment_no, product = :product, inspection_date = :inspection_date, active = :active WHERE id = :equipment_id");
$stmt->bindParam(":equipment_id", $equipment_id,PDO::PARAM_INT);
$stmt->bindParam(":type", $type,PDO::PARAM_STR);
$stmt->bindParam(":acquisition", $acquisition,PDO::PARAM_STR);
$stmt->bindParam(":calibration_interval", $calibration_interval,PDO::PARAM_STR);
$stmt->bindParam(":equipment_no", $equipment_no,PDO::PARAM_STR);
$stmt->bindParam(":product", $product,PDO::PARAM_STR);
$stmt->bindParam(":inspection_date", $this->formatDateStrToTimeStamp($inspection_date),PDO::PARAM_STR);
$stmt->bindParam(":active", $active,PDO::PARAM_INT);
return $stmt->execute();
}
formatDateStrToTimeStamp函數:
private function formatDateStrToTimeStamp($inspection_date)
{
$day = substr($inspection_date,0,2);
$month = substr($inspection_date,3,2);
$year = substr($inspection_date,6,4);
return date('Y-m-d H:i:s', strtotime($year."-".$month."-".$day));
}
如您所見,我已經將我的Inspection_date與代表我要更新的時間戳的字符串的綁定切換了出去。 我在不更新時間戳的情況下測試了該語句,然后它按預期運行。 一旦添加時間戳(在我的情況下,我已插入靜態時間戳),該行將不會更新,並且執行不會返回(它應該返回true或false)。
這是我的表結構:
CREATE TABLE `equipment` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`type` text NOT NULL,
`acquisition` text NOT NULL,
`calibration_interval` text NOT NULL,
`equipment_no` text NOT NULL,
`product` text NOT NULL,
`inspection_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`active` int(11) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
問題: mariaDB中的時間戳是否有所不同,因為自從切換以來,我沒有對代碼進行任何更改,而且我只是從MySQL數據庫的導出中導入了數據庫。
調試完褲子后(因為我不太擅長調試Web應用程序),我終於找到了解決我問題的方法。
PDO的bindparam必須將變量綁定到占位符或問號 ,這在pdo文檔中也有說明。 以我為例,我嘗試在綁定時直接插入字符串,而帶有錯誤的原始代碼使用了時間戳格式化程序的返回值。 在這兩種情況下,綁定到我的占位符時都沒有使用變量,因此出現了錯誤...。
當我使用Chrome的Advanced Rest Client調試功能時遇到了該錯誤,該錯誤顯示了一個錯誤: “只能通過引用傳遞變量” 。
解決方案1:
$inspect = $this->formatDateStrToTimeStamp($inspection_date);
$stmt->bindParam(":inspection_date", $inspect,PDO::PARAM_STR);
解決方案2:
正如瑞安·文森特(Ryan Vincent)在評論中指出的那樣,請改為使用bindValue(有關更多靈感,請參閱他的評論)
但是仍然有些困惑:我還是有點困惑,因為代碼以前在其他主機上運行沒有問題。 我不記得PHP版本或其他任何內容,但是如果有人可以確認以前的版本是可能的,它將解釋原因...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.