2014-01-18 6 views
0

안녕하세요, 저는 함수를 호출 할 때 오류가 있습니다.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']; 
    } 
+0

while 루프에서 'die (var_dump ($ row));'를 시도해보십시오. 그러면 다른 모든 실행이 중지되고 화면에 오류가 표시됩니다. –

+2

get_short_posts 함수에서 첫 번째 반복 이후에 반환되므로 foreach가 예상대로 작동하지 않습니다. –

+1

테이블 열 이름을 표시하십시오. 아마도 "Id"또는 "ID"라고 불리는 열 "id"일까요? –

답변

1

당신은 당신이 get_short_posts 기능 오른쪽 첫 번째 반복 한 후 반환 알고, 그래서 예상대로 foreach는 작동하지 않습니다 .

시도 :

<?php 
function get_short_posts() { 
    $sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5"; 
    $result = mysql_query($sql); 
    $return = array(); 
    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') 
     ); 
    } 
    return $return; 
} 
?> 

<?php 
require_once "functions.php"; 

foreach(get_short_posts() as $_post) { 
    echo $_post['id']; 
} 
?> 

또한, Don't use mysql_* functions in new code. 그들은 더 이상 유지되지 않습니다 and are officially deprecated. red box을 참조하십시오. prepared statements에 대해 자세히 알아보고 PDO 또는 MySQLi - this article을 사용하면 어떤 결정을 내리는 데 도움이됩니다. PDO를 선택하면 here is a good tutorial입니다.

+0

고마워요! 그것은 작동합니다! :) – Settori

+0

당신이 나를 고마워 할 수있는 방법을 받아들이는 것을 잊지 마세요, P –

+0

그건 PDO 권자를 배우는 데 사용되는 자습서입니다 ... 훌륭한 직업 ... –

0
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']); 
     $data [] = array (
      "id" => $row['id'], 
      "title" => $row['title'], 
      "content" => $row['content'], 
      "author" => $row['author'], 
      "date" => $timestamp->format('d-m-Y'), 
      "time" => $timestamp->format('H:i') 
     ); 

    } 
    return $data; 
    } 
,

당신은 루프가 정지하며,이 배열에 데이터를 저장하고 abive 코드처럼 그 배열을 반환, 데이터를 반환

0

코드가 잘못되었습니다. 쿼리가 언급 한대로 데이터를 반환한다고 가정하면 아래에 나와 있습니다.

function get_short_posts() { 
    $sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5"; 
    $result = mysql_query($sql); 
    $rows = array(); 
    $return_data = array(); 
    while($row = mysql_fetch_assoc($result)) { 
     $timestamp = new DateTime($row['date']); 
     $data = array (
      "id" => $row['id'], 
      "title" => $row['title'], 
      "content" => $row['content'], 
      "author" => $row['author'], 
      "date" => $timestamp->format('d-m-Y'), 
      "time" => $timestamp->format('H:i') 
     ); 
     $return_data[] = $data; 
    } 
    return $return_data ; 

} 


require_once "functions.php"; 
    $posts = get_short_posts(); 
    foreach($posts as $key=>$val) { 
     echo $val['id']; 
    } 
관련 문제