-3

I have this function:

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

and on my page I have this:

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

However, the echo is not working at all. How can I grab the data from the function to make it work in the echo?

Vlammuh
  • 157
  • 2
  • 11

2 Answers2

4

Just return $row. It contains all the data.

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

And

$res = get_news($_GET['id']);
echo $res['title'] . '<br />' . $res['message'];
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0

Returning the array was enough.

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

return $row;
}

In your page :

<? php
      print"<pre>";
      print_r(get_news($id));
     print"</pre>";
?>
Falt4rm
  • 915
  • 6
  • 21