www.webdeveloper.com
Results 1 to 6 of 6

Thread: can't display data from mysql

  1. #1
    Join Date
    Mar 2013
    Posts
    2

    can't display data from mysql

    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';
    ?>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,220
    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

    eBookworm.us

  3. #3
    Join Date
    Mar 2013
    Posts
    2
    i am selecting from two tables and when i echo the error i get this Unknown column 'users.id' in 'field list'

  4. #4
    Join Date
    Mar 2013
    Posts
    19
    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']).";";

  5. #5
    Join Date
    Apr 2010
    Posts
    88
    I think that the error message is pretty clear. There is no id field in table users.

  6. #6
    Join Date
    Aug 2012
    Location
    Lahore
    Posts
    5

    Smile

    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles