Editing a ResultSet values

Discussion in 'PHP' started by tomerb, Aug 27, 2008.

  1. #1
    Hello all,

    I need to edit values in a resultSet got from a SQL query.

    I've tried referecing it like this:
    foreach ($temp as &$val)
    {
    $val["place"] = "SOMETHING";
    }
    But I'm getting "An iterator cannot be used with foreach by reference".

    I also tried to clone the $temp before doing that and I get a weird error instead.

    Can someone help me reach my goal?
    Thanks.
     
    tomerb, Aug 27, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Try this:
    
    foreach ($temp as $key => $val)
    {
        $temp[$key]["place"] = "SOMETHING";
    }
    
    PHP:
     
    nico_swd, Aug 27, 2008 IP
  3. Dman91

    Dman91 Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have a question, why did he use & (i know that is for reference values) but why in for loop
     
    Dman91, Aug 27, 2008 IP
  4. tomerb

    tomerb Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It doesnt work... :(

    I think we have to invoke it by reference to be able to change it's values.
    I tried this to check:
    $a = array(array(1), array(2), array(3), array(4), array(5), array(6));
    foreach ($a as &$poop)
    {
    $poop[0]++;
    }
    print_r($a);

    and it works great. print (2..3..4..5..6..7)...

    But doing the same thing with a ResultSet doesnt work.
    I thought it might be read only or something like that...
     
    tomerb, Aug 27, 2008 IP
  5. Dman91

    Dman91 Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Edit : Sorry i misunderstood the code
     
    Dman91, Aug 27, 2008 IP
  6. tomerb

    tomerb Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I did it because I need to change the actual values of $temp.
    Otherwise i'd be changing values of copies of the cells of $temp.
     
    tomerb, Aug 27, 2008 IP
  7. tomerb

    tomerb Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes I am...
    $poop[0]++;
    is the same as writing:
    $poop[0] = $poop[0] + 1;
     
    tomerb, Aug 27, 2008 IP
  8. Dman91

    Dman91 Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I know that is same as writing that, i just said edited saying i misunderstood the code.
     
    Dman91, Aug 27, 2008 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    Did you try my code? Give me a print_r() of $temp (in [code] [/code] tags please.)
     
    nico_swd, Aug 27, 2008 IP
  10. tomerb

    tomerb Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Well it's a ResultSet so it wont help a lot:

    MySQLResultSet Object
    (
        [fetchmode:protected] => 1
        [conn:protected] => MySQLConnection Object
            (
                [database:private] => test
                [transactionOpcount:protected] => 0
                [dblink:protected] => Resource id #65
                [dsn:protected] => Array
                    (
                        [compat_assoc_lower] => 
                        [compat_rtrim_string] => 
                        [database] => test
                        [encoding] => 
                        [hostspec] => ***.***.***.***
                        [password] => *********
                        [persistent] => 
                        [phptype] => mysql
                        [port] => 
                        [protocol] => 
                        [socket] => 
                        [username] => root
                    )
    
                [flags:protected] => 0
                [lastQuery] => SELECT threads.THREAD_ID, threads.THREAD_ID AS THREAD_ID, threads.TEXT AS TEXT, IF (date_format(GREATEST(IFNULL(threads.LAST_REPLIED,threads.CREATED_AT),IFNULL(threads.CREATED_AT,threads.LAST_REPLIED)), '%d%m%y') = date_format(CURRENT_DATE(), '%d%m%y'),date_format(GREATEST(IFNULL(threads.LAST_REPLIED,threads.CREATED_AT),IFNULL(threads.CREATED_AT,threads.LAST_REPLIED)), '%H:%i'),IF ((DATE_SUB(CURRENT_DATE(), INTERVAL 6 MONTH) > GREATEST(IFNULL(threads.LAST_REPLIED,threads.CREATED_AT),IFNULL(threads.CREATED_AT,threads.LAST_REPLIED))),date_format(GREATEST(IFNULL(threads.LAST_REPLIED,threads.CREATED_AT),IFNULL(threads.CREATED_AT,threads.LAST_REPLIED)), '%b %y'),IF ((date_format(DATE_ADD(GREATEST(IFNULL(threads.LAST_REPLIED,threads.CREATED_AT),IFNULL(threads.CREATED_AT,threads.LAST_REPLIED)), INTERVAL 1 DAY), '%d%m%y') = date_format(CURRENT_DATE(), '%d%m%y')),'yesterday',date_format(GREATEST(IFNULL(threads.LAST_REPLIED,threads.CREATED_AT),IFNULL(threads.CREATED_AT,threads.LAST_REPLIED)), '%b %D')))) AS LAST_MODIFIED, users.USERNAME AS USERNAME, countries.FLAG_FILENAME AS FLAG_FILENAME, IF (ISNULL(online_users.LAST_TIMESTAMP),'offline.jpg','online.jpg') AS STATUS_FILE, 0 AS RANK FROM (threads, users, countries) LEFT JOIN online_users ON (threads.USER_ID=online_users.USER_ID) WHERE threads.ROOM_ID='4' AND threads.FATHER_ID IS NULL  AND threads.USER_ID=users.USER_ID AND users.COUNTRY_ID=countries.COUNTRY_ID ORDER BY GREATEST(IFNULL(threads.LAST_REPLIED,threads.CREATED_AT),IFNULL(threads.CREATED_AT,threads.LAST_REPLIED)) DESC LIMIT 9, 20
            )
    
        [result:protected] => Resource id #126
        [cursorPos:protected] => 0
        [fields:protected] => 
        [lowerAssocCase:protected] => 
        [rtrimString:protected] => 
    )
    Code (markup):
     
    tomerb, Aug 27, 2008 IP