i am having trouble with my php code where i want to display all the topics from mysql database but it don't display anything and just echo's he posts could not be displayed, please try again later. could someone advise me about what i am doing wrong here thanks. session is in header.php
PHP Code:
<?php
include 'mysql.php';
include 'header.php';
//retrieving basic data from topic
$sql = "SELECT
id,
subject
FROM
topics
WHERE
id = ". mysql_real_escape_string($_GET['id']);
$result = mysql_query($sql);
if(!$result)
{
echo 'The topic could not be displayed, please try again later.' . mysql_error();
}
else
{
if(mysql_num_rows($result) == 0)
{
echo 'This topic dose not exist.';
}
else
{
while($row = mysql_fetch_assoc($result))
{
//display post data
echo '<table class="topic" border="1">
<tr>
<th colspan="2">' . $row['subject'] . '</th>
</tr>';
}
//fetch the posts from the database on post.postby = users.id tells us that these are both the same through left join.
$sql = "SELECT
users.id,
post.content,
post.date,
post.topic,
post.postby,
users.username
FROM
post
LEFT JOIN
users
ON
post.postby = users.id
WHERE
post.topic = " . mysql_real_escape_string($_GET['id']);
$result = mysql_query($sql);
if(!$result)
{
echo '<tr><td>The posts could not be displayed, please try again later.</tr></td></table>';
}
else
{
while($row = mysql_fetch_assoc($result))
{
echo '<tr class="topic-post">
<td class="user-post">' . $row['username'] . '<br/>' . date('d-m-Y H:i', strtotime($row['date'])) . '</td>
<td class="post-content">' . htmlentities(stripslashes($row['content'])) . '</td>
</tr>';
}
}
if(!$_SESSION['loggedIn'])
{
echo '<tr><td colspan=2>You must be <a href="signin.php">signed in</a> to reply. You can also <a href="signup.php">sign up</a> for an account.';
}
else
{
//show reply box
echo '<tr><td colspan="2"><h2>Reply:</h2><br />
<form method="post" action="reply.php?id=' . $row['id'] . '">
<textarea name="reply-content"></textarea><br /><br />
<input type="submit" value="Submit reply" />
</form></td></tr>';
}
//finish the table
echo '</table>';
}
}
include 'footer.php';
?>
What does the mysql_error() part of your output say?
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
change this line
$result = mysql_query($sql);
to
$result = mysql_query($sql) or die(mysql_error());
And write error that you'll get here so I can help more.
However you didn't semicolon to your sql statement. Some versions may cause error for it. You can try this:
$sql = "SELECT
id,
subject
FROM
topics
WHERE
id = ". mysql_real_escape_string($_GET['id']).";";
i will advise in a way that please use this in your query before
PHP Code:
mysql_query($sql);
PHP Code:
echo $sql;
mysql_query($sql);
Execute your page and see what does this query string returns, try to utilize that in SQL section of your database PHPMYADMIN, it will give you the error.
You can easily sort out that where is the line that is creating problem
Bookmarks