SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot Christiano's Avatar
    Join Date
    Apr 2005
    Location
    Belfast, North of Ireland
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Perl Equivalent to Rowcount

    Hey Folks,

    This is just a general question and kind of a small problem. Thing is I am not really after the number of rows but moreso wondering if the rows are greater than zero; and haven't currently seen an elegant way of doing such. I am using Perl:BI, sample of code is shown below. My perl experience is miniscule (Started on monday!) so please be gentle!

    Code:
    sub get_rowcount {
    
            my $sql_rowcount =  qq { SELECT *
            		     	  FROM table
                                	 WHERE number = '$number'
                                  };
    
            my $sth_rowcount = $database_connection->prepare($sql_rowcount) or die "Could not prepare statement: " . $database_connection->errstr;
               $sth_rowcount->execute or die "Could not execute statement: " . $sth_rowcount->errstr;
    
            if (rowcount is geater than zero) {
    
                    # We have more than one row here, lets begin querying them
    
            } else {
    
                   # We have no records, lets insert one!
            }
    }
    Regards,
    MC
    Innovative Design and Development Services
    Design: XHTML - CSS - Photoshop CS - Flash 7 - Illustrator
    Development: Java - JavaScript - PHP - MySQL - .NET - Struts 2

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    you need a different approach. selecting all of the records from a table just to count them is extremely wasteful. use select count(*) from table instead.
    Check out our new Industry News forum!
    Keep up-to-date with the latest SP news in the Community Crier

    I edit the SitePoint Podcast

  3. #3
    SitePoint Zealot Christiano's Avatar
    Join Date
    Apr 2005
    Location
    Belfast, North of Ireland
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree it is a wasteful approach, I had opted to use select count(1) which was okay. But the thing is if the count is above 0 then I am going to need the rows anyway.

    I was looking for something straight forward but that doesn't appear to be an option in perl!

    Thanks,
    MC
    Innovative Design and Development Services
    Design: XHTML - CSS - Photoshop CS - Flash 7 - Illustrator
    Development: Java - JavaScript - PHP - MySQL - .NET - Struts 2

  4. #4
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do it such that if there are rows, your perl code will run and therefore output whatever you need to. if there are no rows it won't run.

    Your code seems, however, to be missing that part where it executes the query and builds either a hash or an arry from the data returned.

    read up further down the dbi docs for things such as:

    while (my @fields = $sth->fetchrow_array){
    }

    That puts the returned data into an array which you can then process. look up putting it into a hash or a hashref.

    if there is data returned, the loop will process it. if there is no data returned the loop has nothing to process and so, it gives you the same benefits as (I think) your count would have done. easy??

    hth
    Edit:

    You may get more help in the perl forum. Also as a side issue; I am confused by your sig. In what country is the Belfast you refer to?


    bazz
    Last edited by IBazz; Apr 16, 2009 at 10:28.

  5. #5
    SitePoint Member
    Join Date
    May 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You definitely don't need to use "SELECT * FROM ..." because you don't need that data, you need only RowCount.
    Code:
    $st = "SELECT count(*) FROM $table WHERE number=$number";
    my ($n) = $dbh->selectrow_array($st);
    if ($n > 0) {
    } else {
    }

  6. #6
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,276
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by ibazz
    Also as a side issue; I am confused by your sig. In what country is the Belfast you refer to?
    Offtopic: it's likely "Belfast, in the North of Ireland", rather than "Northern Ireland is north of (real) Ireland... though who knows how political it gets? : ) /0fftopic

  7. #7
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol.

    I was curious because can you imagine someone from far afield, thinking perhaps that Belfast had sunk - not just the Titanic. just shows how so many local sayings just do not translate to a global stage.

    Better stop here you you can't live too far from Orange and we don't know where this could go.

    bazz


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
  •