2014-01-18 45 views
0

嗨我在調用函數時出錯。PHP - 帶數組的foreach函數

「警告:非法串偏移 'ID' 在C:\ XAMPP \ htdocs中\上線博客\ posts.php 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,並使用PDOMySQLi - 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; 
    } 

你會返回數據,所以循環停止,將數據保存在一個數組中,並像返回代碼一樣返回該數組。

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']; 
    }