Reputation: 367
I am trying to display a dynamic page with PHP and Mysql. It's just a simple blog that has a blog main page and links that go to the blog posts. When clicking on the posts I get a blank page. I think t may be my query thats the issue.
This is the code I'm using when a user clicks on a blog post link to read more:
I can see the correct ID goes into the URL just fine but nothing displays. Any ideas?
$db = new mysqli('localhost', 'root', 'root', 'app');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = <<<SQL
SELECT *
FROM `news`
WHERE `id` = '".mysql_real_escape_string($_GET['id'])."')
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
echo '<h2>' . $row['title'] . '</h2>' . ' ' . $row['description'] . '<br /><br />';
}
echo 'Total results: ' . $result->num_rows;
// Free result set
mysqli_free_result($result);
mysqli_close($db);
?>
Just in case you need to see, here is the main page of the blog:
<?php
$db = new mysqli('localhost', 'root', 'root', 'app');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = <<<SQL
SELECT *
FROM `news`
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
echo '<h2>' . $row['title'] . '</h2>' . ' ' . $row['description'] . '<br /><br />';
echo '<a href="/blogger/showblog.php?id='.$row['id'].'">'.$row['title'].'</a><br /><br />';
}
echo 'Total results: ' . $result->num_rows;
// Free result set
mysqli_free_result($result);
mysqli_close($db);
?>
Upvotes: 1
Views: 371
Reputation: 2658
You have a syntax error when using EOF because you cannot break and concatenate with quote or double quote.
$sql = <<<SQL
SELECT *
FROM `news`
WHERE `id` = '".mysql_real_escape_string($_GET['id'])."')
SQL;
You should do:
$id=mysql_real_escape_string($_GET['id']);
$sql = <<<SQL
SELECT *
FROM `news`
WHERE `id` = $id)
SQL;
As for the blank page, I think you should get a error so make sure you have display_errors and error_reporting set accordingly so you see errors on the page.
I think with something like this should work:
ini_set("display_errors", 1);
error_reporting(E_ALL);
Upvotes: 1