www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Back to basics SQL count

  1. #1
    Join Date
    May 2012
    Posts
    60

    resolved [RESOLVED] Back to basics SQL count

    I cant seem to get this to return a number, just resource id #3.

    this is my code that detects the users search and displays the appropriate.


    It all works great except that the new count line doesn't show a number.

    PHP Code:
        $con mysql_connect($mysql_host$mysql_user$mysql_password);
        
    mysql_select_db($mysql_database$con); 
        
        if ( isset (  
    $_SESSION['order'] ))  $order $_SESSION['order']; else $order 'price';
        if ( isset (  
    $_SESSION['type'] ) && $_SESSION['type'] != 'all')  $type $_SESSION['type']; else $type null;    
        if ( isset (  
    $_SESSION['subtype'] ))  $subtype $_SESSION['subtype']; else $subtype null;    
        if ( isset (  
    $_SESSION['subsubtype'] ))  $subsubtype $_SESSION['subsubtype']; else $subsubtype null;
        if ( isset (  
    $_SESSION['colour'] ))  $colour $_SESSION['colour']; else $colour null;
        if ( isset (  
    $_SESSION['thickness'] ))  $thickness $_SESSION['thickness']; else $thickness null;
        if ( isset (  
    $_SESSION['shape'] ))  $shape $_SESSION['shape']; else $shape null;    
        
        if (isset (
    $type)) $search $search " WHERE type = '" $type "'";
        if (isset (
    $subtype)) $search $search " AND subtype = '" $subtype "'";
        if (isset (
    $subsubtype)) $search  $search "AND subsubtype = '" $subsubtype "'";
        if (isset (
    $colour)) $search  $search " AND colour = '" $colour "'";
        if (isset (
    $shape)) $search  $search " AND shape = '" $shape "'";
        if (isset (
    $thickness)) $search  $search " AND thickness = '" $thickness "'";
        
        
    $finalsearch "SELECT * FROM Items " $search " ORDER BY " $order " DESC";
        
    $count "SELECT COUNT(*) FROM Items " $search;
        
    $itemcount mysql_query($count);  
        
    $data mysql_query($finalsearch);
        
        echo 
    "<br />" $count "-->" $itemcount "<br/>";
        return 
    $data;} 

    the line "echo "<br />" . $count . "-->" . $itemcount . "<br/>";"
    returns :
    SELECT COUNT(*) FROM Items WHERE type = 'plecs' AND subtype = 'celluloid' AND colour = 'blue'-->Resource id #3

    I think i'm just underslept again

  2. #2
    Join Date
    May 2012
    Posts
    60
    also when i enter that generated sql code into phpadmin, it works.

  3. #3
    Join Date
    Mar 2011
    Posts
    1,160
    Replace this:
    Code:
        $itemcount = mysql_query($count);
    With this:
    Code:
        $result= mysql_query($count);
        $row = mysql_fetch_assoc($result);
        $itemcount = $row['COUNT(*)'];

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    You need to "fetch" the result from the query result resource (e.g.: mysql_fetch_assoc() or mysql_result()).
    "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

  5. #5
    Join Date
    May 2012
    Posts
    60
    Thanks Nog.

Thread Information

Users Browsing this Thread

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

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