MySQL PHP 的语法问题

MySQL syntax issue with PHP

当 运行 下面的代码时,我似乎遇到了以下错误。我不确定为什么它在我的本地主机上运行良好,只是不存在:(

There was an error running the query ERROR02 [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 3]

这是我的脚本运行。 ($id)

似乎有问题
function display_article() {
$db = new mysqli('localhost', 'user', 'pass', 'database');

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

$id = mysql_real_escape_string($_GET['id']);
$sql = <<<SQL
    SELECT *
    FROM `article_img`
    WHERE `img_id` = ($id)
SQL;

if(!$result = $db->query($sql)){
    die('There was an error running the query ERROR02 [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
    echo '<div class="article_title">';
    echo '<h2>' . $row['title'] . '</h2>';
        echo '<div class="article_date">';  
        echo 'Posted ' . $row['date']. ''; 
        echo '</div>';
    echo '</div>';

    echo '<div class="article_image">';
    echo '<img src="'.$row['img_1'].'" alt="" width="584" height="368"/>';
    echo '</div>';

    echo '<div class="article_description">';
    echo '<p>' . $row['description'] . '</p>';
    echo '</div>';

    echo '<br /><br />';
    echo '<div class="article_image">';
    echo '<img src="'.$row['img_2'].'" alt="" width="584" height="368"/>'; 
    echo '</div>';
    echo '<br /><br />';
    echo '<div class="article_image">';
    echo '<img src="'.$row['img_3'].'" alt="" width="584" height="368"/>'; 
    echo '</div>';
    echo '<br /><br />';
    echo '<div class="article_image">';
    echo '<img src="'.$row['img_4'].'" alt="" width="584" height="368"/>'; 
    echo '</div>';
    echo '<br /><br />';
    echo '<div class="article_image">';
    echo '<img src="'.$row['img_5'].'" alt="" width="584" height="368"/>'; 
    echo '</div><br />';

}

$id_related = mysql_real_escape_string($_GET['make']);
$sql_related = <<<SQL
    SELECT *
    FROM `article_img`
    WHERE `make` != '$id_related' ORDER BY RAND() DESC LIMIT 2 
SQL;

if(!$result = $db->query($sql_related)){
    die('There was an error running the query ERROR03[' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
echo '<a class="article_related_link" href="/article.php?id='.$row['img_id'].'&make='.$row['make'].'">';
    echo '<div class="article_related">';
            echo '<img src="' . $row['img_url'] . '" width="282" height="174"/>';
    echo '</div>';
echo '</a>';



}
echo '<div class="article_footer">';

echo '</div>';

// Free result set
mysqli_free_result($result);

mysqli_close($db);
}

$id 不会在 HEREDOC 块中计算。

将您的代码更改为类似这样的代码以对其进行正确评估:

$id = (int) $_GET['id'];
$sql = "
    SELECT *
    FROM `article_img`
    WHERE `img_id` = ($id)
";