SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can't figure out why this isn't working...

    PHP Code:
    $sql 'SELECT visitor_name, comment, COUNT(id) as comment_id
                    FROM ' 
    VISITOR_TABLE;
            
    $result $db->sql_query($sql);
            
    $id_count = (int) $db->sql_fetchfield('comment_id');
            if(
    $id_count == 0)
            {
                
    $template->assign_var('NO_COMMENT''<p>There are no comments</p>');
            }
            else
            {
            
                while(
    $row $db->sql_fetchrow($result))
                {
                    
    $template->assign_block_vars('visitor', array(
                        
    'NAME'        =>    $row['visitor_name'],
                        
    'COMMENT'    =>    $row['comment'],
                    ));
                }
            } 
    The if statement works just fine, but when I add data to the database the else isn't working, although I got a feeling that I'm screwing up somehow within the while statement... anybody got any ideas?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    if your question is php related, perhaps you could try the php forum

    however, your query looks bad, and it appears you aren't checking for syntax errors

    are you trying to count comments for each visitor?

    you should really test your query first, outside of php, before putting it into your php code
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, i wasn't sure which forum to put it in as it has both PHP and SQL in it, but anyway, I'm trying to show the data in the database, and if there isn't any data show the "no comments"

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    okay, click on the little red flag under your avatar and ask the moderators to move this thread to the php forum

    meanwhile, remove the COUNT(id) as comment_id from the query
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Once I removed that part of the query, the data from the database showed up, but if I delete the data the "no comment" doesn't show up anymore.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You are using a Database Abstraction Layer that I am not familiar with, so I'd suggest something a bit simpler with the introduction of a counter.

    PHP Code:

    $sql 
    'SELECT visitor_name, comment FROM ' VISITOR_TABLE
            
    $result $db->sql_query($sql); 
            
    $count 0// start a counter in PHP

                
    while($row $db->sql_fetchrow($result)) 
                {  
                    
    $count++;  // add one to the counter for each iteration

                    
    $template->assign_block_vars('visitor', array( 
                        
    'NAME'        =>    $row['visitor_name'], 
                        
    'COMMENT'    =>    $row['comment'], 
                    )); 
                } 
            
     
    // if the counter is still at 0 then do your other thing
     // always try and do a strict check with three ===
    if( $count === ){

        
    $template->assign_var('NO_COMMENT''<p>There are no comments</p>'); 


    There is probably an easier way to test the number of rows returned, but this ought to work *UNTESTED*

  7. #7
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Whatever class you're using to access your DB should have a method that will tell you the number of results returned. Something similar to mysql_num_rows, PDOStatement::rowCount, or mysqli_stmt_num_rows.
    Keith
    Freelance web developer
    http://www.duvalltech.com/


Bookmarks

Posting Permissions

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