Jump to content

Can't view database data on website using PHP coding


lacey
Go to solution Solved by xProteuSx,

Recommended Posts

I'm trying to view the posts stored in the database but for some weird reason I am only seeing the messahe thats says the post cannot be accessed. Can someone look at the code below and see if you can point out where i have gone wrong please.

 

<?php


include 'connect.php';
include 'header.php';
echo "<div class='inside'>";
echo '<h3>Category View</h3><br />';
                    
//retrieve the category from the database based on $_GET['cat_id']
$sql = "SELECT
            cat_name,
            cat_description
        FROM
            category
        WHERE
            cat_id = " . $_GET['id'];

$result = mysql_query($sql) or die("<p>Could not execute query. Sorry for the inconvenience, please try again later.</p>");
        
if($result)
{        
    if(mysql_num_rows($result) == 0) {
        echo '<p>There are no topics in this category yet.</p>';
    }
    else {
        $row = mysql_fetch_assoc($result);
        
        echo
        '<table width="100%" class="category">
            <tbody>
                <tr>
                    <th colspan="2" class="tableheader">' . $row['cat_name'] . '</th>
                </tr>
                <tr>
                    <th class="leftpanel">Topic</th>
                    <th class="rightpanel" align="center">Last Post</th>
                </tr>';
                
                $sql = "SELECT    
                            topic_id,
                            topic_subject,
                            topic_date,
                            topic_cat
                        FROM
                            topic
                        WHERE
                            topic_cat = " . mysql_real_escape_string($_GET['id']) ."
                        ORDER BY
                            topic_date
                        DESC";
                
                $result = mysql_query($sql,$connect) or die('The topics could not be displayed, please try again later.<br /><br />');
                
                if(mysql_num_rows($result) == 0) {
                    echo 'There are no topics in this category yet.';
                }
                else {
                    //prepare the table
                    while($row = mysql_fetch_assoc($result)) {    
                        $sql = "SELECT    
                                    post_id,
                                    post_date,
                                    post_by
                                FROM
                                    post
                                WHERE
                                    post_topic = " . $row['topic_id'] ."
                                ORDER BY
                                    post_date
                                DESC
                                LIMIT
                                    1";
                        
                        $result2 = mysql_query($sql,$connect) or die('The post could not be accessed, please try again later.<br /><br />');
                        $row2 = mysql_fetch_assoc($result2);    
                                
                        echo '<tr>';
                            echo '<td class="leftpart">'
                                .'<h4><a href="topic.php?id=' . $row['topic_id'] . '">' . $row['topic_subject'] . '</a><br /><h4>'
                                .'</td>'
                                .'<td class="rightpart">'
                                .'<span class="byline"><span class="created">'
                                .istoday($row2['post_date'])
                                .'</span><br/><span class="floatright">by '
                                . $row2['post_by']
                                .'</span></span></td>';
                        echo '</tr>';
                    }
                }
                
        echo    
            '</tbody>
        </table>';
    }
} echo "</div>";

include 'footer.php';
?>

Link to comment
Share on other sites

  • Solution

What error do you get as output?  You've got some error checking code in there, but you don't specify it to help us out.  I suggest that you take a look at the MySQL query itself.  As you've got it it is:

 

$sql = "SELECT
            cat_name,
            cat_description
        FROM
            category
        WHERE
            cat_id = " . $_GET['id'];

 

I think that might be the problem.  Try something like this:

 

$sql = "

SELECT cat_name, cat_description

FROM category
WHERE cat_id = $_GET[id]";

 

I think the issue might have been your use of " and ' within the query.  Hope this helps.  Cheers!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.