Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: count()

  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    count()

    hi all,

    ok the scenario is like this.




    let's say i have a few records in my db. what i want to do is i want to count the number of records in the and then +1 to the number and then insert the new value(with the added 1) in to the DB. i have done something like this. but it's not working and i am getting this notice:



    Notice: Undefined variable: num in c:\program files\apache group\apache\htdocs\ewedding\uploadagain.php on line 235




    this is m code:



    PHP Code:

    $query 
    "SELECT * FROM tblgallery WHERE username = '" .$_POST["txtUsername"]. "' AND pic_Num = 1";
      
    $rs mysql_query($query);
      
    $numRows mysql_num_rows($rs);

    while (
    $row mysql_fetch_object($rs))
        {
          
    $num count($row mysql_fetch_object($rs));
           if (
    $num 0)
          {
            
    $num 1;
          }
          else 
          { 
           
    $num $num 1;
          } 
        }

    $query "INSERT INTO tblgallery (pic_Num) VALUES ('" $num "')";
           
    mysql_query($query) or 
           die (
    mysql_error()); 


    and i checked the DB that it didn't insert the value. pls help.thank you
    Warm Regards,
    Mivec

  • #2
    New Coder
    Join Date
    Apr 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Don't do this:
    PHP Code:
    $row mysql_fetch_object($rs
    inside the while loop - It's already the condition for the while loop, and if you **** around with the $row var inside the loop, it's bound to bugger everything up.

    zigo

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $query "SELECT * FROM tblgallery WHERE username = '" .$_POST["txtUsername"]. "' AND pic_Num = 1"
      
    $rs mysql_query($query); 
      
    $numRows mysql_num_rows($rs); 

    while (
    $row mysql_fetch_object($rs)) 
        { 
          
    $num count($row mysql_fetch_object($rs)); 
           if (
    $num 0
          { 
            
    $num 1
          } 
          else  
          {  
           
    $num $num 1
          }  
        } 
    should be replaced by
    PHP Code:
    $query "SELECT count(*) as numrec FROM tblgallery WHERE username = '" .$_POST["txtUsername"]. "' AND pic_Num = 1"
    $rs mysql_query($query) or die ('Queryproblem'); 
    $row mysql_fetch_assoc($rs)) 
    $num $row['numrec']; 
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #4
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for replying..but raf..i dun really understand ur code. especially at

    die ('Queryproblem');
    what do u mean queryproblem? and y do u use mysql_fetch assoc and not mysql_fetch_object? so sorry...i am pretty new to php that's y. i hope u can explain ur code...thank you thank you...

    PHP Code:
    $query "SELECT count(*) as numrec FROM tblgallery WHERE username = '" .$_POST["txtUsername"]. "' AND pic_Num = 1";  
    $rs mysql_query($query) or die ('Queryproblem');  
    $row mysql_fetch_assoc($rs))  
    $num $row['numrec']; 
    Warm Regards,
    Mivec

  • #5
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i dun really understand ur code. especially at
    Quote:

    die ('Queryproblem');

    what do u mean queryproblem?
    or die ('Queryproblem') will print 'Queryproblem' if the mysql_query() function returns an error, and will then immedeately stop processing the script. So this means, if you have an invalid sql. Like if you would have a statement like 'select var from table testing asc' . If the sql is valid, then nothing is printed.
    and y do u use mysql_fetch assoc and not mysql_fetch_object?
    Because i handle it as an associative array instead of as an object. No point in treating a 1 value recordset as an object. But it's basically just a matter of preference.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

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