I seem to be getting the following error when running the below code. I'm not sure why though as it works fine on my localhost, just not live :(
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]
Here is the script I'm running. It seems to be having an issue with the ($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 won't be evaluated in a HEREDOC block.
Change your code to something like this to have it properly evaluated:
$id = (int) $_GET['id'];
$sql = "
SELECT *
FROM `article_img`
WHERE `img_id` = ($id)
";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.