Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    Okay... so I've got this code
    Code:
    $sql = "UPDATE $statstable SET views = (views + 1) WHERE postID = $postid";
    	$result = mysql_query($sql);
    	if( @!mysql_affected_rows($result) && $postid){ // Creates a new row if none exists
    		$sql = "INSERT INTO $statstable (postID, views) VALUES ('$postid', 1)";
    		mysql_query($sql);
    	}
    Now what it's doing (which is really quite interesting) is this:

    If there is no row, it creates one with postID and views =1. Then it will create another row with postID and views = 2.

    If there is a row, it updates all the rows (now two) and then it adds another row.


    Why is the @!mysql_affected_rows($result) returning false if there's clearly rows? (as the query affects rows in the database).

    I think I might also have an issue where if you call an update statement, then an insert statement both are called in the end - that's no problem but I need this multiple rows thing to sort out =/

    Thanks
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  2.  

  3. #2
    Senior Member jlgosse's Avatar
    Join Date
    Jan 2004
    Location
    Newfoundland, Canada
    Posts
    1,037
    Member #
    4570
    Liked
    7 times
    Brak, why not do a select statement to check for the rows, and then, depending on whether or not values are found, the system would update or insert.

    I think that would work effectively.


  4. #3
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    Wouldn't my way be a bit more efficient? I was thinking of doing that... but for 99% of the time this function is just going to update a row. So it seems to me that I should just update it, and it would fail if there's no rows (but apparently I might be wrong). I think I'll have to do that.
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  5. #4
    Member randomaccess's Avatar
    Join Date
    Aug 2004
    Location
    Nottingham, UK
    Posts
    40
    Member #
    7168
    Hi

    The first problem I notice is mysql_affected_rows($result). That function takes a LINK IDENTIFIER, not a RESULT RESOURCE ! So it will fail every time. Because you've put an @ in front, you're not seeing the error that would have given you this clue.

    There's no need to put the @, as affected_rows shouldn't cause any output when used correctly.

    The second problem is your use of single and double quotes. Is postid a string or a number? In the INSERT you've treated it like a string by surrounded it with single quotes; in the UPDATE you've treated it like a number by not surrounding with quotes.

    Assuming it's a number, try the code like this:

    PHP Code:
    /*check $postid HERE to prevent sql errors*/
    if ($postid) {
      
    $sql "UPDATE $statstable SET views = (views + 1) WHERE postID = $postid";

      
    $result mysql_query($sql);

      if (
    $result) {
         if( !
    mysql_affected_rows($result) ) {
        
    $sql "INSERT INTO $statstable (postID, views) VALUES ($postid, 1)";
        
    mysql_query($sql);
         }
      }
    else {
         
    /* will only reach here if a database error has prevented the UPDATE working */
        
    }



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:13 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com