Page 1 of 1

Editing a ResultSet's values

Posted: Wed Aug 27, 2008 5:12 am
by tomerb
Hello all,

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

I've tried referecing it like this:

Code: Select all

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.

Re: Editing a ResultSet's values

Posted: Wed Aug 27, 2008 6:01 am
by marcth
Try:

Code: Select all

foreach($records as $key=>$record) {
  $records[$key] = 'New Value';
}
Looks like your record set is an object implementing the Iterator interface. I bet there's a method in your db object to get the records as an array.

Re: Editing a ResultSet's values

Posted: Wed Aug 27, 2008 7:23 am
by tomerb
This doesnt work since it only changes a local copy of the val.

Re: Editing a ResultSet's values

Posted: Wed Aug 27, 2008 7:25 am
by tomerb
I'm sorry, I didnt get what you wrote correct.

It gives me an error: " Cannot use object of type MySQLResultSet as array in "...
Problem.

Re: Editing a ResultSet's values

Posted: Wed Aug 27, 2008 7:46 am
by jayshields
Which means that you cannot use a foreach loop on your ResultSet object. As said before, the class you're using probably has it's own iterator or a method to convert the ResultSet object to an array.

Re: Editing a ResultSet's values

Posted: Wed Aug 27, 2008 8:21 am
by marcth
What's the output for

Code: Select all

 print gettype($temp)
 
print '<pre>';
print_r($temp);
print '</pre>';

Re: Editing a ResultSet's values

Posted: Wed Aug 27, 2008 11:11 am
by tomerb
gettype= object
get_class= MySQLResultSet

Code: Select all

MySQLResultSet Object
(
    [fetchmode:protected] => 2
    [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, decode_tb_escape(highlight_words(threads.TEXT, 'bar')) 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, (get_rank_by_delimiter('bar', threads.TEXT, ' ', 10, 5) + get_rank_by_delimiter('bar', users.USERNAME, ' ', 10, 5) + COALESCE((SELECT SUM(get_rank_by_delimiter('bar', t.TEXT, ' ', 5, 1) + get_rank_by_delimiter('bar', users.USERNAME, ' ', 5, 1)) FROM    threads t WHERE  t.FATHER_ID = threads.THREAD_ID), 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 RANK DESC,GREATEST(IFNULL(threads.LAST_REPLIED,threads.CREATED_AT),IFNULL(threads.CREATED_AT,threads.LAST_REPLIED)) DESC LIMIT 30
        )
 
    [result:protected] => Resource id #126
    [cursorPos:protected] => 0
    [fields:protected] => 
    [lowerAssocCase:protected] => 
    [rtrimString:protected] => 
)

Re: Editing a ResultSet's values

Posted: Wed Aug 27, 2008 11:13 am
by tomerb
One more thing..
I am able to iterate through it, I do it on other parts of my app, but I only vie the fields, I never change them.

Re: Editing a ResultSet's values

Posted: Wed Aug 27, 2008 11:17 am
by marcth
Cool! Check this out: http://creole.phpdb.org/docs/api/creole ... ltSet.html.

There is a method called

Code: Select all

 
MySQLResultSet->getArray()
 
You'll prolly want to use that somehow. The object also has a setFetchmode() method, which might prove to be useful as well!

Re: Editing a ResultSet's values

Posted: Wed Aug 27, 2008 12:14 pm
by tomerb
Thanks a lot,
but i've already been through all that plenty.
getArray - returns 1 column, not the entire ResultSet as an array.
setFetchMode - just sets the method that you access the arrays with.

I dont think there's a way to change the values of the resultSet... :?

Re: Editing a ResultSet's values

Posted: Wed Aug 27, 2008 12:27 pm
by marcth
Well then just do:

Code: Select all

$records = array();
$record = array();
 
while(MySQLResultSet->getArray($record)} {
  $records[] = $record;
}
 
foreach($records as &$record) {
  &record = "Something";
}
 

Re: Editing a ResultSet's values

Posted: Wed Aug 27, 2008 12:37 pm
by tomerb
Yeah... I wanted to avoid that. :)

I think I prefer just solving it with a MySql function that does the text manipulation I need.

Thanks a lot for the help.