query($sql); $nextNum = 1; if ($row = $result->fetch_assoc()) { $lastNumStr = substr($row['projectNumber'], strrpos($row['projectNumber'], '-') + 1); $nextNum = intval($lastNumStr) + 1; } return $prefix . str_pad((string)$nextNum, 4, '0', STR_PAD_LEFT); } }