MYSQL和PHP新闻系统[关闭]

I have this news system but I can't figure out how to do it like this: news.php?id=1 then it will output the news id 1. Please help.

I have this so far:

<?php
include_once('includes/config.php');
if($id != "") {
    $id = mysql_real_escape_string($id);
    $sql = mysql_query("SELECT * FROM news WHERE id = '$id'");
}
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)){
if(isset($_GET['id']));
    echo $res['body'];
}
?>

It connects to the database (details are stored in the config).

the parameters after the ? in the URL are GET items. Use this:

<?php

if (isset($_GET['id'])) {

    $id = $_GET['id'];
    // Rest of your code

}
<?php
include_once('includes/config.php');

// see if the id is set in the URL (news.php?id=)
if(isset($_GET['id'])) {

    // get the ID from the URL
    // to make it safer: strip any tags (if it's a number we could cast it to an integer)
    $id = strip_tags($_GET['id']);

    // don't use SELECT *, select only the fields you need
    $sql = mysql_query("SELECT body FROM news WHERE id=".mysql_real_escape_string($id));

    while($row = mysql_fetch_assoc($sql)) {
        echo $res['body'];
    }
} else {
    echo 'please select an article';
}

I would recommend you get away from using the mysql functions and use mysqli instead, as mysql is depreciated and you'll have to learn mysqli or PDO anyway.

Edit: updated code per comments

Firstly lets dissect your current code, to see where your going wrong.

<?php
include_once('includes/config.php');
/*
$id is not set anywhere before its used so this if statement will not fire,
if you are attempting to get this $id from a url parameter then you need
to set it first from $_GET['id'] global
*/
if($id != "") {
    $id = mysql_real_escape_string($id);
    $sql = mysql_query("SELECT * FROM news WHERE id = '$id'");
}
/*
This piece of code will fire but where is $sql set?
The mysql_query() function expects a string containing your sql query
so the subsequent lines of code will fail because of this
*/
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)){
    //this block is in the wrong place
    if(isset($_GET['id']));
    echo $res['body'];

}
?>

The idea is to get the user input E.G the $_GET['id'] from the url first, check the value is what your looking for, and then build your query.

As the mysql_* functions are deprecated I will show you an example using PDO. Though you can use mysqli, BUT you must always use prepared query's whenever user values come into contact with your database. This is to stop nasty/accidental sql injections.

<?php 
// make the connection to the database using PDO
try {
    $db = new PDO('mysql:host=127.0.0.1;dbname=the_awsome_db', 'yourusername', 'password');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $db->exec("SET CHARACTER SET utf8");

} catch(PDOException $e) {
    exit('Sorry there is a problem with the database connection :' . $e->getMessage());
}

// sanitize user input - expecting an int
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);

if (is_numeric($id)) {
    // now lets query the database with the param id from the user

    // prepare the query, using a placeholder
    $stmt = $db->prepare('SELECT body, 
                                 some_other_column 
                          FROM news 
                          WHERE id = :placeholder_id');

    // bind the placeholder with the value from the user
    $stmt->bindParam(':placeholder_id', $id);

    // execute the prepared query
    $stmt->execute();

    // fetch the result
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    // result not empty - display
    if (!empty($result)) {
        // display your result, use print_r($result) to view the whole result set if unsure
        echo $result['body'];
    } else {
        // no matching id found in the db, do something
        echo 'No results found';
    }
} else {
    // do something as user input is not a number
    exit(header('Location: ./index.php'));
}
?>

Hope it helps, if your unsure of getting parameters from the user you may need to look up some more tutorials and get the hang of that first before dabbling with databases and all that good stuff.