从功能中抓取数据

时间:2015-05-26 11:29:23

标签: php

我有这个功能:

function get_news($id) {
    $query      = mysql_query("SELECT `author`, `title`, `message` FROM `news` WHERE `id` = $id");
    $row        = mysql_fetch_array($query);
    $title      = $row['title'];
    $author_id  = $row['author'];
    $message    = $row['message'];
}

在我的页面上我有这个:

get_news($_GET['id']);
echo $title . '<br />' . $message;

然而,回声根本不起作用。如何从函数中获取数据以使其在echo中工作?

2 个答案:

答案 0 :(得分:4)

返回$row。它包含所有数据。

function get_news($id) {
    $query      = mysql_query("SELECT `author`, `title`, `message` FROM `news` WHERE `id` = $id");
    $row        = mysql_fetch_array($query);
    return $row;
}

并且

$res = get_news($_GET['id']);
echo $res['title'] . '<br />' . $res['message'];

答案 1 :(得分:0)

返回阵列就足够了。

  <div class="row">
    <div class="col-xs-10 col-xs-offset-1">
      <h2>
        <%= f.input :title, placeholder: 'Title of the project', label: false, input_html: {class: "title"} %>
      </h2>
    </div>
  </div>

  <div class="row">
    <div class="col-xs-10 col-xs-offset-1">
      <h4>
        <%= f.input :description, as: :text, placeholder: 'Description about this project', label: false %>
      </h4>
    </div>
  </div> 

在您的信息页中:

function get_news($id) 
{
$query = mysql_query("SELECT `author`, `title`, `message` 
                      FROM `news` 
                      WHERE `id` = $id");
$row = mysql_fetch_array($query);

return $row;
}