我的PHP代碼:致命錯誤:調用一個成員函數修改()一個非對象
require_once("config.inc.php");
$conn = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
mysqli_set_charset($conn, 'utf8');
$username = "Michael";
$result = $conn->query("
SELECT *
FROM gebruikers, afdelingen
WHERE gebruikers.afdeling_id = afdelingen.afdeling_id
AND gebruikersnaam = '".$username."'
");
$value = mysqli_fetch_object($result);
$allow_times1 = $value->allow_time;
echo $allow_times1; // output: 09:00,14:00,17:20,21:00
$allow_times2 = "09:00,14:00,17:20,21:00";
echo $allow_times2; // output: 09:00,14:00,17:20,21:00
$myArray = explode(',', $allow_times1);
foreach($myArray as $my_Array) {
$timeFrom = "$my_Array:00";
$date = DateTime::createFromFormat('H:i:s', $timeFrom)->modify("+5 minutes");
}
我得到以下錯誤:
Fatal error: Call to a member function modify() on a non-object in /srv/websites/kabouter/htdocs/test/test.php on line 30
我不會得到錯誤,如果我改變:
$myArray = explode(',', $allow_times1);
到:
$myArray = explode(',', $allow_times2);
爲什麼我得到$ allow_times1的錯誤,而$ allow_times1和$ allow_times2具有相同的回顯輸出?歡迎所有幫助!提前致謝。
你確定爆炸是在30線? – RiggsFolly
也許嘗試明確地將'$ value-> allow_time'強制轉換爲字符串:'$ allow_times1 =(string)$ value-> allow_time;' –
爲什麼要做'$ timeFrom =「$ my_Array:00」;'只需修改'createFromFormat('H:i',$ timeFrom) - > modify(「+ 5分鐘」)的格式;' – RiggsFolly