SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    england
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple count query

    Hi ian trying to do a simple count query, when a member has posted, lets say 50 images, i want to take them to a order page, i have written this code below but freezes the browser,

    PHP Code:
      $totalcount 0;
            
    $query "SELECT count(*) as total from images where userid='$userid' ";        
       
    $result mysql_query($query) or die("Query failed.");    
       while (
    $row mysql_fetch_array($result))

    {

    $total=$row[total];
    }


    if(
    $total>25)
     
    header('Location: order.php');   

     exit(); 

  2. #2
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    try
    PHP Code:
    $result mysql_query("SELECT id FROM images where userid='$userid' ");  
    $row mysql_fetch_array$result );
    $total mysql_numrows($result); 
    What I lack in acuracy I make up for in misteaks

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your origional code is correct.

    Try replacing the header() with var_dump($total);
    Do you get the expected output? Remember, you must send headers before sending any output. You can read the documentation for the header() function for more info.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    lorenw, that is probably the most inefficient method possible
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You are trying to access the returned array incorrectly, you requested an array be returned rather than an associative array.

    PHP Code:
    <?php
    $sSQL 
    sprintf("SELECT COUNT(*) AS total FROM images WHERE userid = %s",
        (Integer)
    $userid
    );
    $rResult mysql_query($sSQL);
    while(
    $aRow mysql_fetch_assoc($rResult))
    {
        
    $iNumberOfImages $aRow['total'];
    }
    if(
    $iNumberOfImages 25)
    {
        
    header('Location: http://www.yourserver.com/order.php');
        exit;
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    * mysql_fetch_array() returns both numerically and associative indexed. So it will work, it's just ever so slightly wasteful.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    *whistles*

    Wow, that goes in my 'things I learnt today' book.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Since your query returns only one line, there is no need for a while-loop.

  9. #9
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    r937, I will read and learn from this thread. I'm glad I posted that so I don't do it that way anymore. I usually use mysql_numrows($result); for checking if no results are returned from the database do this else do that. Is this a wrong thing?

    Thanks
    What I lack in acuracy I make up for in misteaks

  10. #10
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    england
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    i have tried your code SilverBulletUK but it still freezes the browser


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
  •