Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 9 of 9

Thread: Database error

  1. #1
    Senior Member
    Join Date
    Apr 2005
    Location
    Hatfield, England
    Posts
    855
    Member #
    9790
    Wow i have a headache.

    I don't understand why this doesn't work. Could someone have a look at this please.

    This is still a work in progress. The news system was working fine. Then I added a simple if statement to check if the person is logged in. Now the page generates an error.

    You can see the error if you go to www(dot)jasonstanley.co.uk/unc/index.php

    The confusing thing is that if you click on all news then it displays the news article. The function is imported from another script... could this be a factor?

    Heres the relevant php on my site.
    PHP Code:
    <?php
                        
    if( $userdata['session_logged_in'] )
                            {
                                    
    loggedinnews();
                            }
                        else
                            {                    
                                    
    viewnews();
                            }
    ?>
    And here's the viewnews function.

    PHP Code:
    <?php
                
    function viewnews() {                
                    
    /* user config variables */
                    
    $max_items 5/* max number of news items to show */
                    
                    /* make database connection */
                    
    $db mysql_connect ('111','111','111');
                    
    mysql_select_db ('test',$db);
                    
                    function 
    displayNews($all 0) {
                        
    /* bring in two variables
                         * $db is our database connection
                         * $max_items is the maximum number
                         * of news items we want to display */
                        
    global $db$max_items;
                        
                        
    /* query for news items */
                        
    if ($all == 0) {
                            
    /* this query is for up to $max_items */
                            
    $query "SELECT id,title,newstext," 
                                     
    "DATE_FORMAT(postdate, '%Y-%m-%d') as date " 
                                     
    "FROM news ORDER BY postdate DESC LIMIT $max_items";
                        } else {
                            
    /* this query will get all news */
                            
    $query "SELECT id,title,newstext," 
                                     
    "DATE_FORMAT(postdate, '%Y-%m-%d') as date " .
                                     
    "FROM news ORDER BY postdate DESC";
                        }
                        
    $result mysql_query ($query);
                        while (
    $row mysql_fetch_assoc ($result)) {/*line 30----------------*/
                            /* display news in a simple table */
                            
    echo "<TABLE border=\"1\" width=\"580\">\n";
                    
                            
    /* place table row data in 
                             * easier to use variables.
                             * Here we also make sure no
                             * HTML tags, other than the
                             * ones we want are displayed */
                            
    $date $row['date'];        
                            
    $title htmlentities ($row['title']);
                            
    $news nl2br (strip_tags ($row['newstext'], '<a><b><i><u>'));
                            
                            
    /* display the data */
                            
    echo "<TR><TD><b>$title</b> posted on $date</TD></TR>\n";
                            echo 
    "<TR><TD>$news</TD></TR>\n";
                            
                            
    /* get number of comments */
                            
    $comment_query "SELECT count(*) FROM news_comments " .
                                             
    "WHERE news_id={$row['id']}";
                            
    $comment_result mysql_query ($comment_query);
                            
    $comment_row mysql_fetch_row($comment_result);
                            
                            
    /* display number of comments with link */
                            
    echo "<TR><TD><a href=\"{$_SERVER['PHP_SELF']}.
                                 
    "?action=show&id={$row['id']}\">Display Comments</a>" .
                                 
    "($comment_row[0])</TD></TR>\n";
                            
                            echo 
    "<TR><TD><a href=\"{$_SERVER['PHP_SELF']}.
                                 
    "?action=show&post\">Add Comments</a>";
                            
                            
    /* finish up table*/
                            
    echo "</TABLE>\n";
                            echo 
    "<BR>\n";
                        }
                        
                        
    /* if we aren't displaying all news, 
                         * then give a link to do so */
                        
    if ($all == 0) {
                            echo 
    "<a href=\"{$_SERVER['PHP_SELF']}.
                                 
    "?action=all\">View all news</a>\n";
                        }
                    }
                    
                    function 
    displayOneItem($id) {
                        global 
    $db;
                        
                        
    /* query for item */
                        
    $query "SELECT * FROM news WHERE id=$id";
                        
    $result mysql_query ($query);
                        
                        
    /* if we get no results back, error out */
                        
    if (mysql_num_rows ($result) == 0) {
                            echo 
    "Bad news id\n";
                            return;
                        }
                        
    $row mysql_fetch_assoc($result);
                        echo 
    "<TABLE border=\"1\" width=\"300\">\n";
                    
                        
    /* easier to read variables and 
                         * striping out tags */
                        
    $title htmlentities ($row['title']);
                        
    $news nl2br (strip_tags ($row['newstext'], '<a><b><i><u>'));
                        
                        
    /* display the items */
                        
    echo "<TR><TD><b>$title</b></TD></TR>\n";
                        echo 
    "<TR><TD>$news</TD></TR>\n";
                        
                        echo 
    "</TABLE>\n";
                        echo 
    "<BR>\n";
                        
                        
    /* now show the comments */
                        
    displayComments($id);
                    }
                    
                    function 
    displayComments($id) {
                        
    /* bring db connection variable into scope */
                        
    global $db;
                        
                        
    /* query for comments */
                        
    $query "SELECT * FROM news_comments WHERE news_id=$id";
                        
    $result mysql_query ($query);
                        echo 
    "Comments:<BR><HR width=\"580\">\n";
                        
                        
    /* display the all the comments */
                        
    while ($row mysql_fetch_assoc ($result)) {
                            echo 
    "<TABLE border=\"1\" width=\"580\">\n";
                            
                            
    $name htmlentities ($row['name']);
                            echo 
    "<TR><TD><b>by: $name</b></TD></TR>\n";
                        
                            
    $comment strip_tags ($row['comment'], '<a><b><i><u>');
                            
    $comment nl2br ($comment);
                            echo 
    "<TR><TD>$comment</TD></TR>\n";
                            echo 
    "</TABLE>\n";
                            echo 
    "<BR>\n";                    
                        }
                    }    
                        
    /* add a form where users can enter new comments */

                    /* this is where the script decides what do do */
                    
                    
    echo "<CENTER>\n";
                    switch(
    $_GET['action']) {
                        
                        case 
    'show':
                            
    displayOneItem($_GET['id']);
                            break;
                        case 
    'all':
                            
    displayNews(1);
                            break;
                        default:
                            
    displayNews();
                    }
                    echo 
    "</CENTER>\n";
                    echo 
    "<p>Please Login to post a comment";
                }
    ?>

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 times
    What happens if you do this:

    if( $userdata['session_logged_in'] )
    {
    viewnews();
    }
    else
    {
    viewnews();
    }


    Maybe the if statement is always true, and you're
    actually going to loggedinnews when you're thinking
    it's going to viewnews. Try that temporarily to test it.


    .


  4. #3
    Senior Member
    Join Date
    Apr 2005
    Location
    Hatfield, England
    Posts
    855
    Member #
    9790
    Someone told me to add a line so the error is posted.

    The problem was with this line of SQL
    Code:
    "SELECT id,title,newstext," .
    "DATE_FORMAT(postdate, '%Y-%m-%d') as date " .
     "FROM news ORDER BY postdate DESC LIMIT $max_items";
    I have written myself another bit of code that makes it work but i still cannot see what was actually wrong with this. For future reference could someone point out the problem please.

  5. #4
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,656
    Member #
    1234
    Liked
    137 times
    Code:
    "FROM news ORDER BY postdate DESC LIMIT $max_items";
    probably have to change it to:

    Code:
    "FROM news ORDER BY postdate DESC LIMIT " . $max_items . ";
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  6. #5
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    That shouldn't be it, since the ""s will automatically embed the variable. Can you show us your `fixed' code?

  7. #6
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,656
    Member #
    1234
    Liked
    137 times
    sure about that? won't it take $max_items literally in the "before" code I posted, and look for that?
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  8. #7
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    No -- since it's in double quotes, PHP will interpret the variable instead of inserting the string literal '$max_items'.

  9. #8
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,656
    Member #
    1234
    Liked
    137 times
    oooohhhhhh
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  10. #9
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    :-)


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:09 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com