Count = 0 ? Should be 1
Results 1 to 9 of 9

Thread: Count = 0 ? Should be 1

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    30

    Count = 0 ? Should be 1

    Hi Guys,

    My count is echoing the $count variable as 0 when it should be 1. When i run the MySQL Query myself in PHPMYADMIN it returns a row. So why when i echo $count is it showing as 0? Any help would be great. Thanks in advance!

    PHP Code:
    <?php

    require("./databaseconnection.php"); //Get Database Login Information

    //Do ISP Check
    mysql_connect(localhost,$username,$password) or die(mysql_error());
    @
    mysql_select_db($database) or die( "Oops theres an error, our highly trained monkeys have been notified.");
    $query "SELECT * FROM `leads` WHERE `IPCheck` = 'NO'";        
    $result=mysql_query($query) or die(mysql_error());

    while(
    $row mysql_fetch_array($result))
    {
          
    $ipcountry=$row['IPCountry'];
          
    $ipcc=$row['IPCC'];
          
    $ipisp=$row['IPISP'];
          
    $leadid=$row['LeadID'];
          
    $ProgramID=$row['ProgramID'];
          
          
    //Get Valid Consumer ISP
            
    $query2 "SELECT * FROM `ValidISP` WHERE `ISP` = '$ipisp'";
            
    $result2=mysql_query($query2);
            echo 
    $query2."<br/>";
            
    $count=mysql_num_rows($result2);
            echo 
    $count."<br/>";
            
    var_dump($result2);
            echo 
    "<br />";
            if(
    $count==1){
                while(
    $row2 mysql_fetch_array($result2))
                {
                    
    $validisp=$row2['ISP'];
                    
    $validispstatus=$row2['Status'];
                }
            }else {
                
    $validispstatus="MANUALCHECK";
                }
          
    //End
          
            
    if ( $validispstatus == "PASS" ) {
                
    $query3 "UPDATE leads SET IPCheck='PASS' WHERE LeadID='$leadid'";
                
    mysql_query($query3);
            } elseif ( 
    $validispstatus == "FAIL" ) {
                    
    $query3 "UPDATE leads SET IPCheck='FAIL' WHERE LeadID='$leadid'";
                    
    mysql_query($query3);
            } else {
                    
    $query3 "UPDATE leads SET IPCheck='MANUAL' WHERE LeadID='$leadid'";
                    
    mysql_query($query3);
                   }
    }
    mysql_close();
    //End
    echo "ISP Checks completed!";
    ?>
    Example:

    http://www.loudnewmedia.com/images/ispcheck1.jpg

    http://www.loudnewmedia.com/images/ispcheck2.jpg

    Any help would be great please.

  2. #2
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,539
    mysql_num_rows() only counts result rows from SELECT queries. For INSERT or UPDATE queries, you want mysql_affected_rows().
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    30
    Quote Originally Posted by NogDog View Post
    mysql_num_rows() only counts result rows from SELECT queries. For INSERT or UPDATE queries, you want mysql_affected_rows().
    Hi,

    Thanks for your response, but it is a select query (see below)

    PHP Code:
    $query2 "SELECT * FROM `ValidISP` WHERE `ISP` = '$ipisp'"
            
    $result2=mysql_query($query2); 
            echo 
    $query2."<br/>"
            
    $count=mysql_num_rows($result2); 
    I cant work out why its not working, any help would be much appreciated.

    Thank you.

  4. #4
    Senior Member
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    753
    Does var_dump($result2); give any output?
    Ashley Sheridan
    www.ashleysheridan.co.uk

  5. #5
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,539
    Quote Originally Posted by micky007 View Post
    Hi,

    Thanks for your response, but it is a select query (see below)
    ...
    Oops, my poor old eyes were playing tricks on me again.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  6. #6
    Member
    Join Date
    Mar 2011
    Posts
    30
    Quote Originally Posted by Ashley Sheridan View Post
    Does var_dump($result2); give any output?
    Yea it outputs the following:

    resource(4) of type (mysql result)

  7. #7
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,539
    Just in case, are you positive you are connecting to the same database in your script versus in phpMyAdmin? (Yeah, I know: but I've made stupider mistakes. )
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  8. #8
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by micky007
    My count [ ... ] should be 1.
    are you _sure_? just out of curiosity, try this:
    PHP Code:
          //Get Valid Consumer ISP 
            
    $query2 "SELECT * FROM `ValidISP` WHERE `ISP` = '$ipisp'"
            
    $result2=mysql_query($query2); 
            echo 
    $query2."<br/>"
            
    $count=mysql_num_rows($result2); 
            echo 
    $count."<br/>"
            
    var_dump($result2); 
            echo 
    "<br />"

    //  comment this out:
    //        if($count==1){ 
    //  add the following:
            
    if( true ){

                while(
    $row2 mysql_fetch_array($result2)) 
                { 
                    
    $validisp=$row2['ISP']; 
                    
    $validispstatus=$row2['Status']; 

    //  add the following:
                    
    var_dump$validisp );
                    
    var_dump$validispstatus );

                } 
            } 
    Last edited by traq; 01-17-2013 at 02:03 PM.

  9. #9
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,342
    Quote Originally Posted by traq
    PHP Code:
            echo $query2."<br/>"
    And see if that is exactly the same as what you're entering into PHPMyAdmin. Try copying and entering it.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

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
  •