안녕하세요, 저는 함수를 호출 할 때 오류가 있습니다.PHP - 배열을 가진 foreach 함수
"경고 : 'ID'C 오프셋 불법 문자열 : \ XAMPP \ htdocs에 라인 블로그 \의 posts.php \ 28 2"
기능 :
function get_short_posts() {
$sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
$timestamp = new DateTime($row['date']);
return array (
"id" => $row['id'],
"title" => $row['title'],
"content" => $row['content'],
"author" => $row['author'],
"date" => $timestamp->format('d-m-Y'),
"time" => $timestamp->format('H:i')
);
}
전화 :
require_once "functions.php";
$_posts = get_short_posts();
foreach($_posts as $_post) {
echo $_post['id'];
}
while 루프에서 'die (var_dump ($ row));'를 시도해보십시오. 그러면 다른 모든 실행이 중지되고 화면에 오류가 표시됩니다. –
get_short_posts 함수에서 첫 번째 반복 이후에 반환되므로 foreach가 예상대로 작동하지 않습니다. –
테이블 열 이름을 표시하십시오. 아마도 "Id"또는 "ID"라고 불리는 열 "id"일까요? –